1

Why program can't open the file? i.e dies. I searched for this problem, but it seems all fine to me. Funny thing is this code worked before, and i don't think i changed something from that moment in open function.

my $i;
my $regex = $ARGV[0];

for (@ARGV[1 .. $#ARGV]){
    open (my $fh, "<", "$_") or die ("Can't open, $!");
    $i++;
    foreach (<$fh>){
        print "Given regexp: $regex\nfile$i:\n   line $.: $1\n" if $_ =~ /(\b$regex\b)/;
    }
}

OUTPUT: Can't open Not a directory

3
  • 2
    check what file is your script opening, since it doesn't exist .. or die "[$_] $!"; Commented Jun 25, 2014 at 8:36
  • 1
    Try changing the or die to be or die ("Can't open {$_}, $!"). Note the curly braces around $_ so you see exactly what was being opened. Commented Jun 25, 2014 at 8:42
  • @mpapec,thx again, silly mistake i have inputed a wrong argument as ARGV. Commented Jun 25, 2014 at 8:48

2 Answers 2

2

Not a directory means you're supplying an argument that assumes a non-directory is a directory.

For instance, if your argument is

a/b

and

a

exists but is not a directory, you will get this error.

Sign up to request clarification or add additional context in comments.

Comments

1

Check your argument. it should be a proper directory name

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.