Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Introducing Data::Dump::Streamer

by jonadab (Parson)
on Feb 26, 2004 at 13:13 UTC ( [id://331977]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Introducing Data::Dump::Streamer
in thread Introducing Data::Dump::Streamer

Both my version and the current Data::Dumper() allow coderefs to be dumped. My module does it by default, Data::Dumper does it only on request.

I didn't ask about coderefs in general. I asked about closures specifically.


;$;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$;[-1]->();print

Replies are listed 'Best First'.
Re: Re: Introducing Data::Dump::Streamer
by demerphq (Chancellor) on Feb 26, 2004 at 15:15 UTC

    Well, closures are just a special case of code ref, so the answer is yes. Both Data::Dumper and Data::Dump::Streamer can dump closures as they both can dump code references.

    Incidentally this is one of those common misnomers that seem to pop up in Perl circles. Theres a habit of calling "annoymous code refs" as closures, because normally its not obvious that named subroutines can also be closures. And from a perl point of view you never actually interact with a subroutine (beyond calling it) except by reference. So from a given piece of codes POV there is no difference between a "closure", an "anonymous subroutine that isnt a closure" or a "reference to a named subroutine". They are all just references to a CODE object. This holds true for all of perls types pretty much. There is no difference between a "reference to a named array" and an "anonymous array" etc etc.

    sub Foo {print "Foo!\n"} # named subroutine / non-closure my $foo= sub { print "Bar!\n"}; # anonymous / non-closure my $bar= sub { $foo->() }; # anonymous / closure sub FooBar { $bar->() } # named subroutine / closure sub takes_a_callback { my $callback=shift; $callback->(); } takes_a_callback($_) foreach \&Foo,$foo,$bar,\&FooBar;

    In the above case takes_a_callback() can't tell if it what it recieves is a closure or not, nor if its a named subroutine. All it knows that if the object actually is a code ref it will execute it with no arguments. If its isnt a code ref it'll die.

    HTH


    ---
    demerphq

      First they ignore you, then they laugh at you, then they fight you, then you win.
      -- Gandhi


      Data::Dumper doesn't handle closures, in the sense that it makes no effort to provide a lexical environment that will match what the closures have. E.g. (untested):
      sub make_incrementor { my $inc_by = shift; my $value; return sub { $value += $inc_by } } $by2 = make_incrementor(2); $by4 = make_incrementor(4); $by2->(); $by2->(); $by4->(); print Dumper [$by2, $by4];
      This is a hard problem.

        Well, this is actually a problem of B::Deparse and perl overall than it is Dumper or Streamer's fault. There is no way (that I no of, feel free to educate me :-) to know what vars _were_ in the lexical enviornment of the sub, nor to know that when the dump is evaled back whether the user has arranged for these vars to be available or not. IMO this is beyond the requirements of these modules. If you need to use them for serialization of subs then you should take care to manage such things yourself. Personally I only included it to make debugging easier. :-)


        ---
        demerphq

          First they ignore you, then they laugh at you, then they fight you, then you win.
          -- Gandhi


Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-19 06:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found