Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell a Perl function that takes a file to read from the special ARGV handle?

In perldoc perlvar, I read this:

Note that currently "ARGV" only has its magical effect within the "<>" operator; elsewhere it is just a plain filehandle corresponding to the last file opened by "<>". In particular, passing "*ARGV" as a parameter to a function that expects a filehandle may not cause your function to automatically read the contents of all the files in @ARGV.

So, how can I pass *ARGV (or something that resembles it) as a paramter to a function that expects a filehandle, and have that function read all the files in @ARGV?

like image 365
Ryan C. Thompson Avatar asked Oct 29 '10 02:10

Ryan C. Thompson


1 Answers

Go right ahead and pass in the *ARGV typeglob or \*ARGV, its reference. Just make sure that the function eventually making use of it does so via the <$fh> operator or readline($fh), its underlying functional equivalent.

The issue addressed in the cited passage from the perlvar manpage is just trying to remind you that you won't be able to get ARGV’s magic open to trigger if you any use other reading mechanism than readline on the handle, such as read, sysread, or getc.

Run this to prove to yourself it works:

sub mycat {
    my $fh = shift;
    print "$ARGV $.: $_" while <$fh>;
}
mycat(*ARGV);

Put that in a file, then run it with several file arguments:

% perl mycat ./mycat //`pwd`/mycat ~/mycat
./mycat 1: sub mycat {
./mycat 2:     my $fh = shift;
./mycat 3:     print "$ARGV $.: $_" while <$fh>;
./mycat 4: } 
./mycat 5: mycat(*ARGV);
///home/tchrist/mycat 6: sub mycat {
///home/tchrist/mycat 7:     my $fh = shift;
///home/tchrist/mycat 8:     print "$ARGV $.: $_" while <$fh>;
///home/tchrist/mycat 9: } 
///home/tchrist/mycat 10: mycat(*ARGV);
/home/tchrist/mycat 11: sub mycat {
/home/tchrist/mycat 12:     my $fh = shift;
/home/tchrist/mycat 13:     print "$ARGV $.: $_" while <$fh>;
/home/tchrist/mycat 14: } 
/home/tchrist/mycat 15: mycat(*ARGV);

See? It works fine.

like image 59
tchrist Avatar answered Oct 20 '22 00:10

tchrist