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

This is my script:
#!/usr/bin/perl -w use strict; my (@name, @phone, @address, @birthday, @salary, @state, @fname, @lnam +e); my ($n, $p, $a, $b, $s, $first, $last); die "Please supply a file...\nUsage: ./adb0011.pl <file>\n" unless my +$file = shift; open (FILE, "<", $file) or die "Failed to open $file!\n"; while (<FILE>) { ($n, $p, $a, $b, $s) = split(/:/); push(@name, $n); push(@phone, $p); push(@address, $a); push(@birthday, $b); push(@salary, $s); } close FILE foreach (@name) { ($first, $last) = split(/\s/); push(@fname, $first); push(@lname, $last); } print "$_\n" foreach (@lname);
When I put the code that shouldn't depend on the filehandle below the filehandle, I get syntax errors.

But If I do it like this:

#!/usr/bin/perl -w use strict; my (@name, @phone, @address, @birthday, @salary, @state, @fname, @lnam +e); my ($n, $p, $a, $b, $s, $first, $last); die "Please supply a file...\nUsage: ./adb0011.pl <file>\n" unless my +$file = shift; open (FILE, "<", $file) or die "Failed to open $file!\n"; while (<FILE>) { ($n, $p, $a, $b, $s) = split(/:/); push(@name, $n); push(@phone, $p); push(@address, $a); push(@birthday, $b); push(@salary, $s); } foreach (@name) { ($first, $last) = split(/\s/); push(@fname, $first); push(@lname, $last); } print "$_\n" foreach (@lname); close FILE
Then everything works fine.

Why can I not put code below the filehandle that should not depend on the filehandle being closed or not?

Replies are listed 'Best First'.
Re: Why is this script giving syntax errors?
by choroba (Cardinal) on Nov 02, 2013 at 18:40 UTC
    Put a semicolon after close FILE.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Why is this script giving syntax errors?
by Kenosis (Priest) on Nov 02, 2013 at 18:42 UTC

    Didn't you mean close FILE; in the first script (ending it with ;)? Since close FILE is the last statement in the second script, the ; is optional.

      kenosis said:
      Since close FILE is the last statement in the second script, the ; is optional.
      The semi-colon is only option if it's omitted from the last line of your code. As the OP pointed out, the last line of the code doesn't stay that way for very long; there is always one more subroutine to be added.

      I view the 'optional' semi-colon the same way I view the 'optional' comma on the last item of a list. I just know that I'll want to add something else later, and since the Perl compiler is forgiving, I just add it automatically.

      Nota Bene: Just because I know that some particular piece of punctuation is optional (in the sense that the Compiler does the right thing if it is present or absent) doesn't automatically mean that I will remember when I am debugging a problem at O'Dark-Hundred with the entire site down. Don't leave land-mines lying around for the next person to stumble over two years later, it may be you....

      Update =====
      Added the quote from kenosis

      ----
      I Go Back to Sleep, Now.

      OGB

        The semi-colon is only option if it's omitted from the last line of your code.

        Perhaps I'm misunderstanding you here, but when I mentioned "statement," I (implicitly) meant a line containing a set of executable commands, since a semi-colon isn't required on any one of the last four lines of the following script:

        use strict; use warnings; print "Hello, world.\n"; # # # #

        My use of the term "statement" may not have been the best, in this context.

        Your Nota Bene bears an especially crucial message and I appreciate you adding it. Thank you, and the message is well noted.

        "The end of the script" is just part of the truth. It actually is "The end of a scope":

        $ perl -MO=Deparse -e'print $a' print $a; -e syntax OK $ perl -MO=Deparse -e'{print $a}' { print $a; } -e syntax OK $ perl -MO=Deparse -e'{print $a}print $a' { print $a; } print $a; -e syntax OK

        Enjoy, Have FUN! H.Merijn
      Wow, I can't believe I missed that. It's always something small and stupid that makes you feel like an idiot for asking. Thanks to both of you.

        You're most welcome, cspctec!

        Wow, I can't believe I missed that. It's always something small and stupid that makes you feel like an idiot...

        Have been there all too often...