<$fh> question

Discussion in 'Perl Misc' started by Neil Shadrach, Oct 20, 2003.

  1. 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 :(
    Neil Shadrach, Oct 20, 2003
    #1
    1. Advertising

  2. Abigail wrote:

    > Neil Shadrach () wrote on MMMDCCII September
    > MCMXCIII in <URL:news:bn0th1$hgg$>:
    > !!
    > !! 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.
    Neil Shadrach, Oct 21, 2003
    #2
    1. Advertising

  3. 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.

    "Neil Shadrach" <> wrote in message
    news:bn2p86$hgg$...
    > Abigail wrote:
    >
    > > Neil Shadrach () wrote on MMMDCCII September
    > > MCMXCIII in <URL:news:bn0th1$hgg$>:
    > > !!
    > > !! 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.
    >
    Joe Minicozzi, Oct 22, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sean
    Replies:
    1
    Views:
    576
    Cowboy \(Gregory A. Beamer\)
    Oct 20, 2003
  2. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    Quick Question - Newby Question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Feb 14, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    637
    Iain Norman
    Feb 16, 2005
  3. =?Utf-8?B?YW5kcmV3MDA3?=

    question row filter (more of sql query question)

    =?Utf-8?B?YW5kcmV3MDA3?=, Oct 5, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    932
    Scott Allen
    Oct 6, 2005
  4. Philip Meyer
    Replies:
    0
    Views:
    407
    Philip Meyer
    Nov 30, 2003
  5. Bit Byte
    Replies:
    1
    Views:
    821
    Teemu Keiski
    Jan 28, 2007
Loading...

Share This Page