X BOL wishing you a very and Happy New year

Alternative content

History

Our Sponsors



Download BioinformaticsOnline(BOL) Apps in your chrome browser.




Perl One liner basics !!: Revision

Perl has a ton of command line switches (see perldoc perlrun), but I'm just going to cover the ones you'll commonly need to debug code. The most important switch is -e, for execute (or maybe "engage" :) ). The -e switch takes a quoted string of Perl code and executes it. For example:
$ perl -e 'print "Hello, World!\n"'Hello, World!It's important that you use single-quotes to quote the code for -e. This usually means you can't use single-quotes within the one liner code. If you're using Windows cmd.exe or PowerShell, you must use double-quotes instead.

I'm always forgetting what Perl's predefined special variables do, and often test them at the command line with a one liner to see what they contain. For instance do you remember what $^O is?
$ perl -e 'print "$^O\n"'linux
It's the operating system name. With that cleared up, let's see what else we can do. If you're using a relatively new Perl (5.10.0 or higher) you can use the -E switch instead of -e. This turns on some of Perl's newer features, like say, which prints a string and appends a newline to it. This saves typing and makes the code cleaner:
$ perl -E 'say "$^O"'linux
Pretty handy! say is a nifty feature that you'll use again and again.