[Kc] Reminder: Meeting Tonight

Garrett Goebel garrett at scriptpro.com
Tue Nov 11 08:29:39 CST 2003


Tonight's meeting will be at 7PM at the Planet Sub on 50th and Main. 

You're mission should you choose to accept it, is to pick a command-line
one-liner and bring it to the meeting and/or post it to the list.

I was thinking about this month's meeting, and thought perhaps it'd be
interesting to cover the perl command line options. The Chicago perl mongers
group recently did a field guide to it, which I'll provide as a handout at
the meeting.

So I'd like to see if people could bring (or post to the list) particularly
clever or interesting examples of things you can do from the command line
with perl. A quick google search on perl one-liners should give you a wide
assortment to choose from ;)

That said, I'd much rather people come without a one-liner than not come at
all. So don't feel obliged. 

The following is an example of what we might expect to see. Though it needed
be documented at all as long as you can explain what your one-liner does.


Example: 

In place editing is as easy as -p -i -e 

perl -p -i -e s/.../.../ filename 

-p places an while loop around your script which automatically reads a line
from the <> diamond operator. 

-i in place edit. Perl automatically removes or renames the input file and
opens and output file using the original name

-e eval. Evaluates a string of text from the command line. 

<> diamond operator. Reads a single line from the files specified on the
command line 

@ARGV magic array variable which holds the command line arguments 

s/.../.../ a string substitution using regular rexpressions 


To change all instances of the word 'foo' to 'bar' in the file fubar.txt... 

perl -p -i -e s/foo/bar/ fubar.txt 

  in effect becomes: 

for my $file (@ARGV) { 
  open IN "<$file"; 
  unlink $file; 
  open OUT ">$file"; 
  while (my $line = <IN>) { 
    $line =~ s/foo/bar/; 
    print OUT $line;  
  } 
  close IN; 
  close OUT; 
} 

Under Windows, you are not allowed to read from an unlinked filehandle.
Conveniently, the -i command line option also allows you to specify the name
of an extension that the original file should be renamed with. So on Windows
you might instead have written:

perl -p -i.bak -e s/foo/bar/ fubar.txt 


Normally you'd enclose the string of text to be evaluated in single
quotation marks. On Windows due to the limitations of the command shell you
must use double quotation marks. In our case, since no whitespace was
required in the eval text... no quotes were required.

Garrett 

-- 
Garrett Goebel 
IS Development Specialist 
ScriptPro                   Direct: 913.403.5261 
5828 Reeds Road               Main: 913.384.1008 
Mission, KS 66202              Fax: 913.384.2180 
www.scriptpro.com          garrett at scriptpro dot com 
  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/kc/attachments/20031111/85b631d5/attachment.htm


More information about the kc mailing list