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
| [reply] |
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
| [reply] [d/l] [select] |
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. | [reply] [d/l] |