in reply to Re^2: Unsuccessful stat on file test
in thread Unsuccessful stat on file test

Your code is almost there, but you have to remove the semicolon:
my @answer; return "File does not exist\n"; # <- extra semicolon! unless -e $_;

The next problem is:

while (<>) {
If your program is named filetest.pl and you call it like:
./filetest.pl *.txt
that loop is actually going to read each line of all the *.txt files in your current directory, and unless each line actually happens to be the name of a file in that directory, your program will correctly tell you that the file does not exists!

Try adding some extra output (what is $_ ?)

cheers

Replies are listed 'Best First'.
Re^4: Unsuccessful stat on file test
by bluethundr (Pilgrim) on Mar 03, 2008 at 05:06 UTC
    By gum! With your help (and the help of the other posters)...I think I've got it! Hard work do pay off! Well, the prog isn't quite finished. There's still some debugging messages in there, and some tidying up to do.

    But basically it is now in a workable state. If I feed the program (which is named ex1-11, short for exercise 1 chapter 11) files with different modes, it will accuratley reflect those modes.

    Here's what I came up with:
    #!/usr/bin/perl use warnings; use strict; sub filetest { my @answer; print "\n\$_[0] now holds $_[0] \n"; return "File does not exist\n" unless -e $_[0]; push @answer, "readable " if -r $_[0]; push @answer, "writable " if -w $_[0]; push @answer, "executable " if -x $_[0]; print "function result: "; print @answer; return @answer; } while (@ARGV) { my $file = shift @ARGV; $file =~ s/\s*$//; print "\$file is $file"; my @answer = filetest($file); #thanks for the style tip! print "\nmain answer: "; print @answer; print "\n"; }
    Where I was falling down (mainly) was in the subroutine where I was confusing $_ with $_[0]. It was a few chapters ago, and sort of leaked out of my brain. There's a lot to retain to this Perl stuff! ;)

    Just wanted to dash off this note as a thanks to everyone who commented. I will try to finish up this program and begin the next one on the train into NYC tommorow.

    G'night, Monks!