Exploratory one-liners with less typing

Here's one for the "stupid shell tricks" category:

I made myself a shell alias today to simplify the times when i want a quick view of how something works in perl:

alias perl1='perl -MData::Dumper -MYAML::Any -MClass::Autouse=:superloader -E "sub D(\$){ say Dumper(shift) } sub Y(\$){ say Dump(shift) }"'

I'm not quite sure about the name, and I may add more helper functions, but a handy alias like this one allows me to type this at the shell:

perl1 -E 'D [File::Spec->path]'

Instead of

perl -MData::Dumper -MFile::Spec -E 'say Dumper( [File::Spec->path] )'

I don't have to type out -MData::Dumper and I can use D instead of say Dumper()

Plus, thanks to Class::Autouse and it's :superloader import tag I don't have to type out Module::Name ("File::Spec" for example) multiple times just to get a quick glance at it's output.

I haven't decided yet if I want to just append " -E" to the alias. That might get in the way if I want the "-n" or "-p" flags or multiple -E's or something...

Anyway... thought that was handy; Thought I'd share.


This is cool.

If you use it often, maybe pi, which has the added benefit that using tab completion in the shell will still give you a space after perl if you don't want to use the alias.

I would also include -Muft8::all. No wide-char warnings.

You could write it as a function instead, then you can do something like

case "$1" in
    -*) break ;;
     *) set -- -E "$@" ;;

so that -E will be prepended to the arguments only if you didn’t include switches in them yourself. (Code untested, too tired.)

Randy, you might want to look at Data::Printer instead of Data::Dumper there.

It is, but not by default. You can turn it on somewhere in the settings, though.

Thanks for this. It's ironic that I use shell aliases all the time, even for small things like 'git status' (gs), 'emacs -nw' (emacs), 'mplayer' (m). Can't believe I have not used it with Perl!

Marc Fontani created a gist based on this, adding some more features https://gist.github.com/1042504. I forked his gist and made https://gist.github.com/1240122. Thanks for posting this cool idea. I had fun hacking on it.

Leave a comment

About Randy Stauner

user-pic perl -C -E 'say"i \x{2764} ",($^X =~ m#([^/]+)$#)'