in reply to Introducing Data::Dump::Streamer

One limitation I've noticed in Data::Dumper is that it doesn't handle closures. e.g.,

my $foo; { my $bar = 'bar'; $foo = sub { return ($bar .= join $bar, @_) } } use Data::Dumper; print Dumper(\$foo);

That doesn't work. At all. (Okay, it prints *something*, but it's not anything *useful*.) Does your module handle this better? Or am I just being unreasonable in expecting to be able to dump something like that?


;$;=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 25, 2004 at 08:41 UTC

    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.


    ---
    demerphq

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


      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

        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