<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="MS Exchange Server version 5.5.2657.73">
<TITLE>November meeting</TITLE>
</HEAD>
<BODY>

<P><FONT SIZE=2>This month's meeting will be at Nov. 11th at 7PM at the Planet Sub on 50th and Main.</FONT>
</P>

<P><FONT SIZE=2>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. </FONT></P>

<P><FONT SIZE=2>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 should be up on their website shortly. I figure I'll track that or something similar down to provide as a handout at the meeting.</FONT></P>

<P><FONT SIZE=2>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 ;)</FONT></P>

<P><FONT SIZE=2>That said, I'd much rather people come without a one-liner than not come at all. So don't feel obliged. </FONT>
</P>

<P><FONT SIZE=2>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.</FONT></P>
<BR>

<P><FONT SIZE=2>Example:</FONT>
</P>

<P><FONT SIZE=2>In place editing is as easy as -p -i -e</FONT>
</P>

<P><FONT SIZE=2>perl -p -i -e s/.../.../ filename</FONT>
</P>

<P><FONT SIZE=2>-p places an while loop around your script which automatically reads a line from the &lt;&gt; diamond operator.</FONT>
</P>

<P><FONT SIZE=2>-i in place edit. Perl automatically removes or renames the input file and opens and output file using the original name</FONT></P>

<P><FONT SIZE=2>-e eval. Evaluates a string of text from the command line.</FONT>
</P>

<P><FONT SIZE=2>&lt;&gt; diamond operator. Reads a single line from the files specified on the command line</FONT>
</P>

<P><FONT SIZE=2>@ARGV magic array variable which holds the command line arguments</FONT>
</P>

<P><FONT SIZE=2>s/.../.../ a string substitution using regular rexpressions</FONT>
</P>
<BR>

<P><FONT SIZE=2>To change all instances of the word 'foo' to 'bar' in the file fubar.txt...</FONT>
</P>

<P><FONT SIZE=2>perl -p -i -e s/foo/bar/ fubar.txt</FONT>
</P>

<P><FONT SIZE=2>&nbsp; in effect becomes:</FONT>
</P>

<P><FONT SIZE=2>for my $file (@ARGV) {</FONT>
<BR><FONT SIZE=2>&nbsp; open IN &quot;&lt;$file&quot;;</FONT>
<BR><FONT SIZE=2>&nbsp; unlink $file;</FONT>
<BR><FONT SIZE=2>&nbsp; open OUT &quot;&gt;$file&quot;;</FONT>
<BR><FONT SIZE=2>&nbsp; while (my $line = &lt;IN&gt;) {</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; $line =~ s/foo/bar/;</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; print OUT $line;&nbsp; </FONT>
<BR><FONT SIZE=2>&nbsp; }</FONT>
<BR><FONT SIZE=2>&nbsp; close IN;</FONT>
<BR><FONT SIZE=2>&nbsp; close OUT;</FONT>
<BR><FONT SIZE=2>}</FONT>
</P>

<P><FONT SIZE=2>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:</FONT></P>

<P><FONT SIZE=2>perl -p -i.bak -e s/foo/bar/ fubar.txt</FONT>
</P>
<BR>

<P><FONT SIZE=2>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.</FONT></P>

<P><FONT SIZE=2>Garrett</FONT>
</P>

<P><FONT SIZE=2>--</FONT>
<BR><FONT SIZE=2>Garrett Goebel</FONT>
<BR><FONT SIZE=2>IS Development Specialist</FONT>
</P>

<P><FONT SIZE=2>ScriptPro&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Direct: 913.403.5261</FONT>
<BR><FONT SIZE=2>5828 Reeds Road&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Main: 913.384.1008</FONT>
<BR><FONT SIZE=2>Mission, KS 66202&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Fax: 913.384.2180</FONT>
<BR><FONT SIZE=2>www.scriptpro.com&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; garrett at scriptpro dot com</FONT>
<BR><FONT SIZE=2>&nbsp; </FONT>
</P>

</BODY>
</HTML>