Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

HOP file iterator

by ady (Deacon)
on Mar 26, 2006 at 16:19 UTC ( [id://539291]=perlquestion: print w/replies, xml ) Need Help??

ady has asked for the wisdom of the Perl Monks concerning the following question:

Brother monks!

I'm stuck in a simple code snippet from HOP (4.3.3), can't quite understand why this spits out *all* lines from the file (<DATA>) and not just the first 2 lines.

Seems the first NEXTVAL($it) slurps the *whole* file instead of just reading one line up to \n, but why is that so?

Any pointers much appreciated!
#!/usr/bin/perl -w use strict; use warnings; sub NEXTVAL { $_[0]->() } sub Iterator (&) { return $_[0] } sub filehandle_iterator { my $fh = shift; return Iterator { <$fh> }; } my $it = filehandle_iterator(*DATA); for (1..2) { print NEXTVAL($it); } __END__ line 1 line 2 line 3 line 4 line 5
Best Regards,
Allan Dystrup

Replies are listed 'Best First'.
Re: HOP file iterator
by BrowserUk (Patriarch) on Mar 26, 2006 at 16:22 UTC

    In a word, context. Try:

    for (1..2) { print scalar NEXTVAL($it); }

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      <blush> yes, Because print takes a LIST, anything in the LIST is evaluated in list context, and any subroutine that you call will have one or more of its expressions evaluated in list context
      Thanks BrowserUk!
      allan
Re: HOP file iterator
by blogical (Pilgrim) on Mar 26, 2006 at 16:24 UTC
    Do you perchance mean
    sub NEXTVAL { $_[0]->($_[1])
    ?

    "One is enough. If you are acquainted with the principle, what do you care for the myriad instances and applications?"
    - Henry David Thoreau, Walden

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://539291]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (1)
As of 2024-04-25 00:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found