cspctec has asked for the wisdom of the Perl Monks concerning the following question:
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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Why is this script giving syntax errors?
by choroba (Cardinal) on Nov 02, 2013 at 18:40 UTC | |
|
Re: Why is this script giving syntax errors?
by Kenosis (Priest) on Nov 02, 2013 at 18:42 UTC | |
by Old_Gray_Bear (Bishop) on Nov 02, 2013 at 21:00 UTC | |
by Kenosis (Priest) on Nov 02, 2013 at 21:43 UTC | |
by Tux (Canon) on Nov 03, 2013 at 11:59 UTC | |
by cspctec (Sexton) on Nov 02, 2013 at 19:08 UTC | |
by Kenosis (Priest) on Nov 02, 2013 at 19:29 UTC |