Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

The following snippet is to set a variable to "PASSED" unless the word "FAILED" appears in a file. This works fine on AIX and Solaris but on Linux Redhat 7.3, the @result_array is empty, resulting in the grep returning 0 (count of FAILED) and thus reports a "PASSED" instead of a "FAILED" :-(
#!/usr/local/it/bin/perl -w # # Create sample file with two # instances of "FAILED" string # $RFILE="example_file"; open(F1,">$RFILE") or die "cannot open $RFILE for creation!\n"; print F1 "FAILED\nPASSED\nPASSED\nFAILED\nPASSED\n"; close(F1); # # Open file for Append AND read # open(RESULT,"+>>$RFILE") or die "cannot open $RFILE\n"; # # Set an array to the filehandle and count the # number of lines with the "FAILED" string # my @result_array = <RESULT>; $number_failures = grep /FAILED/, @result_array; # # Overall status is FAILED if any "FAILED" strings # are found # $OVERALL_STATUS = ($number_failures > 0) ? "FAILED":"PASSED"; print "+-------------------+\n"; print "| |\n"; print "| EO_FAIL_03 $OVERALL_STATUS |\n"; print "| |\n"; print "+-------------------+\n"; close(RESULT);
If I change the open line to:
open(RESULT,"$RFILE")
it works fine! And yes...I do need it to be "+>>" for other operations not shown in this snippet. Any insight on this would be real helpful.

Replies are listed 'Best First'.
Re: open(F1,"+>>$FILE") works on Solaris,AIX but not Linux??
by pfaut (Priest) on Feb 24, 2003 at 21:59 UTC

    An open with mode '>>' should position to end of file. You need to seek to the beginning in order to read from the file. I added two lines to your program and it now appears to work as you would like it to.

    #!/usr/local/it/bin/perl -w # # Create sample file with two # instances of "FAILED" string # use Fcntl qw(SEEK_SET); $RFILE="example_file"; open(F1,">$RFILE") or die "cannot open $RFILE for creation!\n"; print F1 "FAILED\nPASSED\nPASSED\nFAILED\nPASSED\n"; close(F1); # # Open file for Append AND read # open(RESULT,"+>>$RFILE") or die "cannot open $RFILE\n"; seek RESULT,0,SEEK_SET; # # Set an array to the filehandle and count the # number of lines with the "FAILED" string # my @result_array = <RESULT>; $number_failures = grep /FAILED/, @result_array; # # Overall status is FAILED if any "FAILED" strings # are found # $OVERALL_STATUS = ($number_failures > 0) ? "FAILED":"PASSED"; print "+-------------------+\n"; print "| |\n"; print "| EO_FAIL_03 $OVERALL_STATUS |\n"; print "| |\n"; print "+-------------------+\n"; close(RESULT); <code>--- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
Re: open(F1,"+>>$FILE") works on Solaris,AIX but not Linux??
by jasonk (Parson) on Feb 24, 2003 at 21:58 UTC

    I don't have a Solaris or AIX machine to check, but the Linux man page for fopen says that r+ (+<) and w+ (+>) open the file and position the stream at the beginning of the file, while a+ (+>>) positions the stream at the end of the file. So your options are either to open with +>> and then seek to the beginning of the file before reading, or open with +<, read, and then seek to the end of the file before writing (although you will probably end up at the end of the file anyway, if you read the entire contents).

Re: open(F1,"+>>$FILE") works on Solaris,AIX but not Linux??
by runrig (Abbot) on Feb 24, 2003 at 22:00 UTC
    '+>>' is not listed in the open docs, so I wouldn't use it as a supported option. I don't see why you couldn't use '+<', as that is listed in the docs. After opening the file, you can read from it, and/or seek to the end of the file for appending.

    Update: I take it back, I do see '+>>' in the docs, but you would then need to use seek to read from the beginning of the file, as already suggested.