<$fh> question

N

Neil Shadrach

If I have a few lines like this which work fine:

my $fh=$p->{'fh'};
while (<$fh>)
{ # loop contents
}

How do I avoid resorting to temporary variable $fh?
I've had a look at the perlop pages but all is still not clear to me :(
 
N

Neil Shadrach

Abigail said:
Neil Shadrach ([email protected]) wrote on MMMDCCII September
MCMXCIII in <URL:!!
!! If I have a few lines like this which work fine:
!!
!! my $fh=$p->{'fh'};
!! while (<$fh>)
!! { # loop contents
!! }
!!
!! How do I avoid resorting to temporary variable $fh?
!! I've had a look at the perlop pages but all is still not clear to me :(


Parsing of the content of <> is tricky. One way of solving it is
by not using <>, which is just a shorthand for calling readline:

while (defined ($_ = readline $p -> {fh})) {
# Your loop here
}

Thanks for that. Not as pretty as I'd hoped but enlightening nonetheless.
 
J

Joe Minicozzi

Abigail's solution is probably your best bet. The <> operator has two
functions: one is the readline equivalent and the other is file name
globbing (matching a wildcard file pattern). The way Perl decides what you
want is by examining what's between the angle brackets at compile time. If
anything more complicated than a filehandle or a simple scalar variable is
between the angle brackets, Perl will interpret it as a file name globbing
request. Using readline avoids any ambiguity, as does calling glob if you
want file name globbing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top