in reply to Dumping variables but DRY and simple

I have a vim mapping for that kind of thing. Since it's not trivial to get the variable name with perl standard functions I consider that a good workaround to let your editor help you.
imap DUMPER <ESC>^iwarn __PACKAGE__.':'.__LINE__.$".Data::Dumper->Dump +([\<ESC>llyw$a], ['<ESC>pa']);<ESC>

this mapping lets you type
$my_variableDUMPER
which gets expanded to
warn __PACKAGE__.':'.__LINE__.$".Data::Dumper->Dump([\$my_variable], ['my_variable']);

I can only say that I'm using this for many years now and it has saved me so much time, when typing, as well as when trying to find out the source of Dumper statements in the log. (ever worked on team code and getting various $VAR dumps in the log not knowing where they come from?)

Replies are listed 'Best First'.
Re^2: Dumping variables but DRY and simple
by LanX (Saint) on Mar 27, 2010 at 17:42 UTC
    Ok this goes somehow in the direction of code generation like Smart::Comments with the difference that you can still safely use it in production code.

    But then I would rather prefer to insert an eval-macro solution giving me the opportunity to change details dynamically at central place:

    #!/usr/bin/perl my @bla=(0..9); sub dmp_macro { my $var=shift; (my $var2=$var) =~ tr/@%$//d; use Data::Dumper; return "print Data::Dumper->Dump([\\$var],['\*$var2'])" } print dmp_macro('@bla'),"\n"x3; eval (dmp_macro qw/@bla/);

    output:

    print Data::Dumper->Dump([\@bla],['*bla']) @bla = ( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 );

    (OK I admit, it's not only lisp-ish, it's even uglier ;-)

    Cheers Rolf