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

For the code:
$path = "/main/lvs/";<br /> open(CELLFILE,"./instList.in") || die "Can't open cell list file: $!"; +<br /> while (<CELLFILE>) {<br /> $cell = $_;<br /> chomp($cell);<br /> system("rm ./instLvsOut/passfail.tmp");<br /> open(PASSFAIL,">>./instLvsOut/passfail.tmp");<br /> system("ls $path | grep $cell.>./instLvsOut/passfail.tmp");<br /> print "$cell:\n";<br /> @fp0 =<PASSFAIL>;<br /> $fp = $fp0[0];<br /> chomp($fp);<br /> system("cat ./instLvsOut/passfail.tmp");<br /> print "fp is $fp\n";<br /> }

I am getting the following error:
Use of uninitialized value in concatenation (.) or string at instLvsReport.pl line 14
I have already defined $fp by commands "@fp0 =<PASSFAIL>;" and "$fp = $fp0[0];". The command "system("cat ./instLvsOut/passfail.tmp");" properly prints out the content of <PASSFAIL>. Any idea why still I get this error? Please help.

Replies are listed 'Best First'.
Re: "Use of uninitialized value" even though the value is defined
by moritz (Cardinal) on Apr 01, 2011 at 09:52 UTC

    Please always use strict; use warnings; and declare your variables.

    You don't check the success of the second open call. If the open() fails, there will be no lines in @fp0 (same if the file exists but is empty), and $fp will be undef.

    You can also use autodie to prevent such errors.

      Thanks. Still @fp0 =<PASSFAIL> does not seem to accept the content of PASSFAIL. Any idea why?
        open(PASSFAIL,">>./instLvsOut/passfail.tmp")

        Because you are opening the file for output only and you can't read from that filehandle.

        To check whether the passfail.tmp file is empty, I tried the command system("cat ./instLvsOut/passfail.tmp"); just before the line @fp0 =<PASSFAIL>. It displayed the content of the file properly. It was not empty. Any idea why this code does not accept values in @fp0 =<PASSFAIL>
Re: "Use of uninitialized value" even though the value is defined
by jpl (Monk) on Apr 01, 2011 at 11:05 UTC

    You opened PASSFAIL for output. If you want to read from it, you must open it for reading. Do so after invoking system(), or the file will not yet exist, and the open will fail. As others have noted, it's always wise to check the results of open() and log an error message if it fails.

      Thank You! This really works! :)
Re: "Use of uninitialized value" even though the value is defined
by Anonymous Monk on Apr 01, 2011 at 10:01 UTC
    Where in the code do you turn on warnings?

      You can write the following code below the shebang line:

      use strict; use warnings;

      Hope this helps!

      Sumeet Grover.
        Why, when the code is not strict/warnings compliant?
      I am using -w option at the first line.