For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > July 2006 > Other ways to assign a filehandle to a variable?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Other ways to assign a filehandle to a variable?
Roman Daszczyszak

2006-07-27, 6:57 pm

After reading Randal's March 2000 LM column and learning that you can
assign a filehandle to a scalar by doing something like:

open(FILE, $filename) or die "whatever";
$value = *FILE{IO};

I am wondering if there are other, perhaps better, ways of doing this?

FYI, I googled for "assigning filehandle to variable perl" and a
couple other searches, but only came up with pages directing how to
read from a filehandle into a scalar.

Regards,
Roman
Paul Lalli

2006-07-27, 6:57 pm

Roman Daszczyszak wrote:
> After reading Randal's March 2000 LM column and learning that you can
> assign a filehandle to a scalar by doing something like:
>
> open(FILE, $filename) or die "whatever";
> $value = *FILE{IO};
>
> I am wondering if there are other, perhaps better, ways of doing this?


Yes, there are. Do not rely on columns written 6 years ago. These
days, it's *much* preferred to write:

open my $fh, '<', $filename or die "whatever: $!";

Using an undefined lexical in place of the filehandle automatically
assigns that lexical to be a reference to the filehandle. This has
three main benefits:
The lexicals are subject to strict
The filehandle is automatically closed when the lexical goes out of
scope.
The variable can be passed to other subroutines like any other
variable.

Paul Lalli

Dr.Ruud

2006-07-27, 6:57 pm

"Roman Daszczyszak" schreef:

> After reading Randal's March 2000 LM column and learning that you can
> assign a filehandle to a scalar by doing something like:
>
> open(FILE, $filename) or die "whatever";
> $value = *FILE{IO};
>
> I am wondering if there are other, perhaps better, ways of doing this?
>
> FYI, I googled for "assigning filehandle to variable perl" and a
> couple other searches, but only came up with pages directing how to
> read from a filehandle into a scalar.


Use lexical ones in the first place.

open my $fh, '<', $filename or die "open $filename, stopped" ;

--
Affijn, Ruud

"Gewoon is een tijger."


JupiterHost.Net

2006-07-27, 6:57 pm


> Use lexical ones in the first place.
>
> open my $fh, '<', $filename or die "open $filename, stopped" ;
>


And if you're gogint to die you might as well say why :)

.... or die "open $filename failed: $!" ;
Dr.Ruud

2006-07-27, 6:57 pm

"JupiterHost.Net" schreef:

>
> And if you're gogint to die you might as well say why :)
>
> ... or die "open $filename failed: $!" ;


Yes, I meant:

open my $fh, '<', $filename or die "open $filename, stopped $!" ;

The $! begins with "at bla -bla", so the "stopped" glues nicely.

I don't remember where I copied it from, maybe a perldoc.

--
Affijn, Ruud

"Gewoon is een tijger."


Paul Lalli

2006-07-27, 6:57 pm

Dr.Ruud wrote:
> open my $fh, '<', $filename or die "open $filename, stopped $!" ;
>
> The $! begins with "at bla -bla", so the "stopped" glues nicely.


Uh, no it doesn't.

$ perl -e'open my $foo, q{file} or die "failed, stopped $!"'
failed, stopped No such file or directory at -e line 1.

The die() function without a newline will append "at <file> line
<line>", but this has nothing to do with whether or not $! is present.

Paul Lalli

Dave Gray

2006-07-27, 6:57 pm

For posterity:
<http://perl.plover.com/local.html#3...s_Filehandle_Tr>
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com