pauamma: Cartooney crab wearing hot pink and acid green facemask holding drink with straw (Default)
Res facta quae tamen fingi potuit ([personal profile] pauamma) wrote in [site community profile] dw_dev_training2010-08-31 06:34 pm
Entry tags:

Nifty perl trick I learned about today

Context was someone asking why
print "Hello, world!\n" && die;
doesn't output anything. Someone replied with a nifty trick to help figure this out:
perl -MO=Deparse -e 'print "Hello, world!\n" && die;'
(go ahead and try it in your Dreamhack).
sophie: A cartoon-like representation of a girl standing on a hill, with brown hair, blue eyes, a flowery top, and blue skirt. ☀ (Default)

[personal profile] sophie 2010-08-31 11:38 pm (UTC)(link)
Also, if you need to do this trick but don't know what the operator precedence is, you can add ",-p" after the "Deparse" in order to get Perl to print parentheses to show exactly how things get calculated.

One thing, btw - this won't work as a built-in code beautifier because you'll find that some string literals aren't kept; they're transformed to '???'. That's because it's a constant value that Perl optimised away for whatever reason. (The perldoc for B::Deparse explains more.)
pne: A picture of a plush toy, halfway between a duck and a platypus, with a green body and a yellow bill and feet. (Default)

[personal profile] pne 2010-09-01 06:18 am (UTC)(link)
B::Deparse is really fun.

Have you thought about cross-posting this to [community profile] command_liners?
afuna: Cat under a blanket. Text: "Cats are just little people with Fur and Fangs" (Default)

[personal profile] afuna 2010-09-01 10:27 am (UTC)(link)
Oh neat.

(What is the O for? I can find -M in man perlrun, but I can't find -O)
alierak: (Default)

[personal profile] alierak 2010-09-01 01:28 pm (UTC)(link)
It's the argument to -M. "perldoc O" should do something.
afuna: Cat under a blanket. Text: "Cats are just little people with Fur and Fangs" (Default)

[personal profile] afuna 2010-09-02 05:45 am (UTC)(link)
Oh dur. So obvious now, thanks!