throop has asked for the wisdom of the Perl Monks concerning the following question:
I'm trying to bring some old 4.X code up to 5.8. I'm still confused about using strings as filehandles. The tutorial section at File Input and Output says
Of course, with just a little 'improvement' this loses:open(FILE, "data.txt"); #opens data.txt in read-mode while(<FILE>){ #reads line by line from FILE which i +s the filehandle for data.txt chomp; print "Saw $_ in data.txt\n"; #shows you what we have read } close FILE;
Gives the dreaded:use strict; my($foo) = 'FILE'; open($foo, "data.txt"); #opens data.txt in read-mode while(<$foo>){ #reads line by line from FILE which i +s the filehandle for data.txt chomp; print "Saw $_ in data.txt\n"; #shows you what we have read } close $foo; #close the file.
Oddly, looking through the various tutorials, I can't find a good example of creating a filehandle, binding it to a variable, and passing it around (e.g. as an arg to a subroutine.) What's the ''recomended'' way to do this?
thanks,
throop
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Still blurry on variables for filehandles
by kyle (Abbot) on Jan 24, 2007 at 16:16 UTC | |
|
Re: Still blurry on variables for filehandles
by bingos (Vicar) on Jan 24, 2007 at 16:24 UTC | |
|
Re: Still blurry on variables for filehandles
by Errto (Vicar) on Jan 24, 2007 at 16:34 UTC | |
by ikegami (Patriarch) on Jan 24, 2007 at 17:12 UTC |