Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Common Perl Idioms

by atcroft (Abbot)
on Jul 23, 2004 at 20:13 UTC ( [id://376986]=note: print w/replies, xml ) Need Help??


in reply to Common Perl Idioms

I don't know if this is up to the level of some of the other code here, but sometimes, when I want to verify the contents of a few variables (here $var1, @var2, and %var3, for example purposes), I will toss the following into the code:

if (1) { use Data::Dumper; local($| = 1); print Data::Dumper->Dump([\$var1, \@var2, \%var3], [qw(*variable1 *variable2 *variable3)]), "\n"; }

I can then go back and change it to if (0) to turn it off (and leave it there if I expect to need to look at the content of the variables at some point in the future), or remove it entirely once I'm finished.

Don't know if it helps anyone else, but it has proven helpful to me in the past. (And my heartfelt thanks to the monks who led me to using it, after I first started visiting.)

Replies are listed 'Best First'.
Re^2: Common Perl Idioms
by samtregar (Abbot) on Jul 24, 2004 at 16:41 UTC
    Did you know that the 'if(0)' won't inhibit the loading of Data::Dumper, even if the 'use' is inside the 'if'? That's because 'use' operates at compile time before the optimizer has a chance to remove the dead code.

    -sam

      I learnt this the hard way. I used to write:

      if ( $DEBUG ){ use CGI::Carp qw(fatalsToBrowser); }

      thinking that when I put the code into production all my debugging would dissapear with a simple $DEBUG=0; but Carp is always loaded no matter the value of $DEBUG. It took a long while for me to notice the error becuase when run in prod the conditions which would trigger a die didn't normally occur. My bad.

      --
      Do not seek to follow in the footsteps of the wise. Seek what they sought. -Basho

Re^2: Common Perl Idioms
by ihb (Deacon) on Jul 24, 2004 at 00:48 UTC

    What's particularly good with if (FALSE_CONSTANT) { ... } is that the whole block will be optimized away during the compilation phase.

    ihb

    Read argumentation in its context!

Log In?
Username:
Password:

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

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

    No recent polls found