When I put the code that shouldn't depend on the filehandle below the filehandle, I get syntax errors.#!/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);
But If I do it like this:
Then everything works fine.#!/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
Why can I not put code below the filehandle that should not depend on the filehandle being closed or not?
In reply to Why is this script giving syntax errors? by cspctec
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |