[lug] perl question
Ralf Mattes
rm at ns.aura.de
Sun Apr 9 14:54:46 MDT 2000
On Sun, 9 Apr 2000, Michael J. Hammel wrote:
[...]
> open(FD, $filename);
> while ( <FD> )
> {
> blah;
> }
> seek (FD, 0, 0);
> while ( <FD> )
> {
> blah;
> }
> close(FD);
>
> Hey Tony - check me on that <FD> bit. I'm not exactly sure what the <>
> bracketing is for.
Perl black magic ;-) The <> is a special perl construct to
easily access text files (after all this is what perl once was
meant to do). In scalar context the <> construct will return
a line of input at a time, in array context it will return
a list of all lines of the file referenced by the file descriptor.
BTW, the 'File descriptor' isn't really a Unix file descriptor,
it's an opaque data structure that has some 'magic' capabilities.
Once (long ago) the only way to pass an open file to a subroutine
was by passing the typeglob of the file descriptor
(for example 'pretty_print(*MYFILE, $data);'
If you prefer a 'cleaner' approach you could do:
#!/usr/bin/perl
use IO::File;
my $file = new IO::File("/etc/passwd")
or die "Someone removed the password file. Good luck!\n";
while (<$file>){
....
}
Ralf
*-------------------------------------------------------------------*
| | |==
| Ralf Mattes | ralf.mattes at pyramid.de |==
| Programming, Administration | rm at ns.aura.de |==
| | |==
*-------------------------------------------------------------------*==
====================================================================
More information about the LUG
mailing list