Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Dispatch Table and Data::Dumper

by ghenry (Vicar)
on Oct 19, 2005 at 13:23 UTC ( #501295=perlquestion: print w/replies, xml ) Need Help??
ghenry has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I have a snippet of code:

use strict; use warnings; use DBI; use Data::Dumper; use IO::Prompt; use PDF::Reuse; my $choice = prompt 'Please choose your report output format...', -1, -menu => [ 'Console', 'PDF', 'HTML', 'Quit', ]; print "You chose: [$choice]\n\n"; my $action_ref = { 'Console' => \&print_console, 'PDF' => \&generate_pdf, 'HTML' => \&generate_html, 'Quit' => sub { print "Exiting...\n"; exit 0; } }; if ( $action_ref->{$choice} ) { $action_ref->{$choice}->(); } else { warn "Unknown option: '$choice';\n" }

Why does print Dumper($action_ref); give:

$VAR1 = { 'HTML' => sub { "DUMMY" }, 'Quit' => sub { "DUMMY" }, 'Console' => sub { "DUMMY" }, 'PDF' => sub { "DUMMY" } };

and not what's actually in the subs, which are further down the program?

A simple pointer to the docs I missed will do ;-)


Walking the road to enlightenment... I found a penguin and a camel on the way.....
Fancy a Just ask!!!

Replies are listed 'Best First'.
Re: Dispatch Table and Data::Dumper
by Roy Johnson (Monsignor) on Oct 19, 2005 at 13:28 UTC
    Data::Dumper says:
    $Data::Dumper::Deparse or $OBJ->Deparse([NEWVAL])
    Can be set to a boolean value to control whether code references are turned into perl source code. If set to a true value, B::Deparse will be used to get the source of the code reference. Using this option will force using the Perl implementation of the dumper, since the fast XSUB implementation doesn't support it.
    The implication is clearly that by default it doesn't output source code. This is because you can't normally examine coderefs as source. It requires magic, unlike the other data types that Data::Dumper dumps.

    Caution: Contents may have been coded under pressure.
      Note that is a new feature in the latest release(s) of Data::Dumper. The previous version that is available on CPAN, Data::Dumper 2.101, didn't have it yet.

      So it's very well possible that the version of Data::Dumper that came with your perl, doesn't have it yet.

      Oh dear me, perldoc should always be my first point of call. This should have been one for the CB!

      Thanks guys.

      Walking the road to enlightenment... I found a penguin and a camel on the way.....
      Fancy a Just ask!!!
Re: Dispatch Table and Data::Dumper (use Data::Dump::Streamer instead)
by demerphq (Chancellor) on Oct 19, 2005 at 14:19 UTC

    Data::Dumper is incapable of properly dumping closures. You can use it (at least later versions) as an alternate interface to B::Deparse which can dump code, but unfortunately Deparse is only able to tell half the story, that is the source code involved in the subroutine, it is not able to provide the context required to properly recreate a closure. DD should be able to provide the context, but for many reasons it does not.

    Enter Data::Dump::Streamer. DDS is specifically designed to be capable of dumping closures properly. diotalevi and I spent a fair amount of time making the closure dumping code work properly and IMO we have done a pretty reasonable job. In some situations you may find that DDS needs to rename some of the variables you used in your code, but under normal circumstances this isnt required.

    If you have any trouble with using DDS for this please let me know.

    Update: Fixed diotalevis name. :-)


      'd' isn't capitalized when you start a sentence with my name.
Re: Dispatch Table and Data::Dumper
by g0n (Priest) on Oct 19, 2005 at 13:30 UTC
    Data::Dump::Streamer on the other hand will output the code.


    $perlquestion=~s/Can I/How do I/g;

Re: Dispatch Table and Data::Dumper
by phaylon (Curate) on Oct 19, 2005 at 13:28 UTC
    Grep perldoc Data::Dumper for DUMMY, first hit ;)

    Ordinary morality is for ordinary people. -- Aleister Crowley

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://501295]
Approved by Corion
Front-paged by Tanktalus
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (11)
As of 2017-07-24 18:08 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (356 votes). Check out past polls.