in reply to sending data thru a sub routine
The name infile is used for three different types of things: a file name in the first line above, a function name in the second line, and a file handle at the last one. This is at best very confusing for yourself. You could rewrite this as follows:my $infile = $ARGV[0]; infile ($infile); sub infile { open( my $infile, '<', $ARGV[0] ) or die "cannot open file: $!"; # ...
At least there is no danger of mixing up the various entities. Actually, although the $infile name is perfectly acceptable, it might be even better to have a name reflecting the content of the file, such as, for example $resources_infile or $employees_infile, whatever you have in the file, or even simply $resources or $employees. We can see from your code that you are going to open it as a file, but have no idea of the contents. Also naming the file that you cannot open in the message passed to die can be useful when you have to open several files and something goes wrong.my $infile = $ARGV[0]; process_file ($infile); sub process_file { my $current_file = shift; # or: my $current_file = $_[0]; open my $FILEHANDLE, '<', $current_file or die "cannot open file +$current_file: $!"; # ...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: sending data thru a sub routine
by james28909 (Deacon) on May 11, 2014 at 19:21 UTC |