in reply to if/while/else problem

I'm thinking you are using -s wrong. The code if ( -s file ){ checks for the existence of a file associated with the filehandle file. You either need to provide the file name (a string) or the correct file handle (MYINPUTFILE). This would have been caught had you included use strict;use warnings.

In addition, you should really be using the three-argument form of open and testing to see if your open operations fail. Fixing the block structural issues mentioned above, perhaps you mean:

system "rm file2"; open(FILE2, ">", "file2") or die "Opening output failed: $!"; my $file = "file"; if ( -e $file and -s $file ){ open(MYINPUTFILE, "<", $file) or die "Opening input $file failed: +$!"; while(<MYINPUTFILE>) { my($line) = $_; chomp($line); print FILE2 "$line\n"; print FILE2 "you need to do this\n"; print FILE2 " \n"; } } else { print FILE2 "There is nothing to do\n"; }

Replies are listed 'Best First'.
Re^2: if/while/else problem
by ikegami (Patriarch) on Mar 25, 2009 at 20:13 UTC

    This would have been caught had you included use strict;use warnings.

    With not one but three warnings, even!

    Unquoted string "file" may clash with future reserved word Name "main::file" used only once: possible typo -s on unopened filehandle file
Re^2: if/while/else problem
by ddrew78 (Beadle) on Mar 25, 2009 at 20:53 UTC
    Thank you kennethk, this one did the trick. And thanks to all who gave suggestions, I was really stumped on this one.