Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Why are closures cool, continued?

by trs80 (Priest)
on Jan 19, 2002 at 03:19 UTC ( #139995=note: print w/ replies, xml ) Need Help??


in reply to Why are closures cool, continued?

From my understanding of closures, yes, the above sample is
a closure. I reread the Camel (2nd ed. pg. 253) and the
example given there is similar, but the one above has added
an additional sub (doit) to the mix. It also states that
"By and large, closures are not something you need to trouble
yourself about."
The purpose however is to allow for something to run in the
in the context it was initially called outside of that
context. Which means you are creating an anonymous sub
that will retain its initial values and when you access
it at a later point it will "remember" it had a value.

In the above example $dave is equal to a sub, it
appears that Data::Dumper doesn't handle closures since
I ran $dave through the Dumper and it gave me a value of
sub { "DUMMY" }
hmmmm

Any way interesting chunck of code.


Comment on Re: Why are closures cool, continued?
(ichimunki) re x 2: Why are closures cool, continued?
by ichimunki (Priest) on Jan 19, 2002 at 04:47 UTC
    Data::Dumper is going to give out "DUMMY" as the value for any sub, not just a closure. The contents of a subroutine are represented internally with a variety byte code I believe, which would look like so much nonsense if it were printed in the contents of a Data::Dumper dump. Contrast to scalars (or hash keys or hash/array values made from scalars) and to references. Scalars eventually turn out to be some sequence of characters, and references inside a data structure can be represented with brackets and braces.

    'perldoc Data::Dumper' has more info on this in the BUGS section.

      As to "[t]he contents of a subroutine [being] represented internally with a variety [of] byte code", you're both right and wrong (as is so often the case when talking about internals). The internals do deal with coderefs as pointing to a bunch of bytecode, but that bytecode is not inherently serializable; it has lots of pointers in it. Moreover (without resorting to Devel:: or B:: modules), it isn't accessable with Perl code.

      The perl6 analog of Data::Dumper will be able to dump coderefs as Parrot assembler (pasm); one of the cool side-effects of going to an explicit VM.

      Thanks,
      James Mastros,
      Just Another Perl Scribe

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2014-12-25 23:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (163 votes), past polls