PGTS PGTS Pty. Ltd.   ACN: 007 008 568

point Site Navigation

point Other Blog Threads



  Valid HTML 4.01 Transitional

   Give Windows The Boot!
   And Say Goodbye To Viruses!

   Ubuntu

   If you own a netbook/laptop~
   Download Ubuntu Netbook!






PGTS High and Mighty Blog

Thread: Perl Programming

Author Image Gerry Patterson. The world's most humble blogger
Edited and endorsed by PGTS, Home of the world's most humble blogger

The Things We Like About Perl


Chronogical Blog Entries:



Date: Thu, 22 May 2008 22:30:21 +1000

Every now and then I re-read the perl man pages and re-discover something that I had forgotten. As I re-read the perlrun section, I am reminded that perl, the powerful, eclectic, slightly crazy scripting engine that we all (sort of) know and love, is not only useful as a programming tool, but can be used for one-liners. These are the little (usually unix) commands that can accomplish so much, with so little effort.

Even though perl one-liners are powerful, I will admit that sometimes I have eschewed perl one-liners in favour of the more convenient and succinct awk and cut one-liners. For example this neat little one-liner, which can be found in the perlrun man page:

	perl -ane 'print pop(@F), "\n";'

Can be done with the following awk one-liner:

	awk '{print $NF}'

Which (I find) is easier to remember and to type. If you won't take my word for it, try them out! You will see that they both yield identical results. i.e. both one-liners read STDIN and write to STDOUT only the last word of what they read.

And the following perl one-liner (also in the perlrun section):

	 perl -lpe 'substr($_, 80) = ""'

Is all very well. But it blows up if any of the lines are less than 80 characters long. And in any case I think this is much easier:

	 cut -c 1-80

Or (for the die-hard awkers) even this more verbose one-liner is slightly easier to type:

	 awk '{print substr($0,1,80)}'

Both the awk and cut alternatives are robust and they trim the file to 80 characters ... They don't self-destruct if any of the lines are less than 80 characters long.

I suppose the reason I prefer awk is because I am often working inside vi, rather than at the command prompt. And therefore I prefer one-liners that are easy to remember and type, and which read from STDIN and write to STDOUT and put a line terminator ('\n') at the end of each line, without having to be told to do so. And awk just does exactly that! Perl on the other hand requires a "-n" "-p" or "-l" switch, combined with a "-e" switch (for the perl expression). And the perl autosplit switch will split a line into $F[0], $F[1] ... etc which is more tedious to type than than the awk equivalents of $1, $2 .. etc.

And reading the perlrun man page, reminds me of the -i switch. How could I forget this handy little switch? This allows you to do things like this:

	perl -pi -e 's/bar/baz/' fileA

The above one-liner was copied from the perlrun man page. And it's hard to trump that with an awk or sed one-liner. Using the "-i" switch at the command line can be a powerful way of changing files and making a backup copy. For example this command:

	perl -pi'.bak' -e 's/\bbar\b/baz/ig' *.html

Will change the single word "bar" into "baz", irrespective of case. However it will leave things like "foobar" "barbar" and "barcan" alone. It will operate only on ".html" files and it will make a backup copy. Not bad for a one-liner at the command prompt!

Furthermore it will even work on read-only files (although some might consider that a flaw!). And, if you thought this one-liner was particularly convenient, and something that you might use regularly, you could make an alias for it with this command:

	 alias sub='perl -pi".bak" -e '

You could even put it in your .profile (or .bashrc if you are modern Linux type of programmer). From now on if you wanted to change some files with a perl extended regular expression, you could do something like this:

	 sub 's/foo/bar/g/' *.txt

Although, if you are cautious, you might first run the command:

	grep foo *.txt | less

Just to check on what you are about to do.

You can also use perl one-lines in Windows ... Although you might want to enclose them in double-quotes. The following one-line can be convenient for transforming '\' in '/' for use in pathnames to be used inside Activestate perl:

	perl -npe "s/\\(\w+)/\/$1/g"

Other Blog Posts In This Thread:

Copyright     2008, Gerry Patterson. All Rights Reserved.