Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Iterator to parse multiline string with \\n terminator

by Laurent_R (Canon)
on Oct 06, 2013 at 08:58 UTC ( #1057130=note: print w/replies, xml ) Need Help??


in reply to Iterator to parse multiline string with \\n terminator

Hi,

as a side note, your anonymous function does not really act as a closure:

my $fh = IO::File->new($filename, 'r'); my $fh_iterator = sub { my $fh = shift; my $line = $fh->getline; } while (my $line = $fh_iterator->($fh)) { # do stuff to $line }

because you are passing $fh each time to the sub (and you have to, you actually get a fresh copy of $fh each time the anonymous subroutine is called).

If you want it to act as a closure, you may do something like this (untested):

sub create_iterator{ my $filename = shift; my $fh = IO::File->new($filename, 'r'); return sub { my $line = $fh->getline; } } my $fh_iterator = create_iterator($file_name); while (my $line = $fh_iterator->()) { # do stuff to $line }

Now, $fh is really a persistent variable within the sub scope, this is a real closure.

Replies are listed 'Best First'.
Re^2: Iterator to parse multiline string with \\n terminator
by three18ti (Scribe) on Oct 06, 2013 at 09:09 UTC

    What exactly makes it not a closure? Is it that I'm passing a variable each time? If it was a new copy of $fh, wouldn't $fh_iterator->($fh) always return the same line (since it's creating a copy of the $fh object, on next passing it would be a copy of the original)?

    Thanks for setting me straight, I always like learning new things.

      $fh is a file handler, i.e. it is actually an iterator on a file, so that each time you read from $fh, you get the next line. In your sub, your my $fh = shift; actually creates a new copy of $fh each time the sub is called. It still works because $fh "knows" which is the next line to read from the file. But your anonymous sub is not a closure; the alternative code I wrote is actually keeping its own copy of $fh, my anonymous sub actually closes on $fh. Please note that an anonymous function is not necessarily a closure, and a closure does not necessarily have to be anonymous (although is is often the case).

      You might want to have a look to this: Closure on Closures.

        Awesome! Thanks for the info and link.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1057130]
help
Chatterbox?
[james28909]: another thing, i used to be atheist. life experiences, personal evidence. and my own perception is the reason why i believe the things i do.
[erix]: and now you are agnostic?
[james28909]: arunbear, your picking bits of history to fit your own narrative. right now, humans are smarter than in any of those times.
[james28909]: in order for there to be a mutation, there has to be a universe with laws setup that even allow a mutation.
[james28909]: everything past the first two things you piked from my posts are irrelevant to me and my beliefs. they are side effects.
[james28909]: two or three. if it has to do with how humans are handling said situations, then that is after they were able to evolve into those problems. agnostic means only believe with evidence. i took that from a tyson video
[james28909]: and it seems that racism suggest that there is indeed a better race, but that is for evolution to decide

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (17)
As of 2017-12-15 14:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (433 votes). Check out past polls.

    Notices?