Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^3: sub that sets $_

by anonymized user 468275 (Curate)
on Jun 28, 2005 at 13:23 UTC ( [id://470617]=note: print w/replies, xml ) Need Help??


in reply to Re^2: sub that sets $_
in thread sub that sets $_

No - each iteration returns a chunk into $_ in the for or foreach loop.

One world, one people

Replies are listed 'Best First'.
Re^4: sub that sets $_
by holli (Abbot) on Jun 28, 2005 at 13:31 UTC
    definitly not.
    use FTest; my $test = new FTest (); print foreach $test->foo; print for $test->foo;
    Meanwhile in a module
    package FTest; sub new { return bless {}, shift; } sub foo { return 1; } 1;
    That code prints "11". I you were correct that would be an endless loop.
      I have to pass there because I don't understand what this has to do with $_ being a default for/foreach parameter.

      One world, one people

        I have to pass there because I don't understand what this has to do with $_ being a default for/foreach parameter.
        Well, nothing. $_ is the default variable for a for/foreach loop. That's not the point.

        The point here is that the return value of a subroutine or method does not set $_ automatically. You suggested to use a for/foreach loop instead of a while loop, and that will not work because the method "chunk" is a iterator that only returns a single scalar per call. Thus, since for/foreach works on lists, the loops block will only be executed once.

        In the while-loop however the block will be executed for every iteration until "chunk" returns false.

        To make for/foreach work here, the ChunkReader-class would have to return a list of chunks and that, as said, is a waste of memory.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (10)
As of 2024-04-23 09:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found