[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl-Scribes] Basic Perl Filters in BBEdit (much revised)



Some general suggestions:

1) I appreciate that some of what Jim Correia said overlapped with 
what I suggested and I certainly don't expect Brian to use any more 
of my suggestions than he wants to, but having said that, I feel that 
some of my deleted material might be useful.  Specifically, I began 
the article with a basic description of how to use (run) a Perl 
filter in BBEdit and how to create one.  Most critically (IMHO), the 
current article doesn't explain that to be available for use, a Perl 
script needs to be placed in a specific folder within the BBEdit 
folder. (:MacPerl Support:Perl Filters:)

2) I think moving the discussion of command line switches was a real 
improvement, but I still think the transition is a little odd, mostly 
because Jim Correia uses them extensively before Brian discusses 
them.  I think this can most easily be rectified by deleting (or 
actually moving) Jim's third example.  The section "TMTOWTDI" 
basically discusses development of that example, and I would place 
this final version at the end of that discussion.

3) I think the section "A Useful One-Line Filter" should come before "TWTOWTDI"

4) The section "TWTOWTDI"; it seems disjointed to me, and some of it 
doesn't seem to fit under this title.  After thinking about it for a 
while, I suggest the following revision:

---===---

<h3>Command Line Switches</h3>

<p>
I've seen more than my share of Perl Filters, and a lot of evidence that
MacPerler's in general are unaware of which #! switches work under MacPerl,
and when and how to use them.  This is really a topic for an entirely
separate column, but I'll try to deal with it briefly here.  Most switches
work with MacPerl.  Of those which might be considered most useful in the
context of Perl Filters:
</p>

<pre>
-n (iterate entire script using &lt;&gt;) works as expected
-p (iterate entire script using &lt;&gt; printing each line) works as
expected
-l (automatically chomp line endings from input
    -- with -n or -p no terminating \n needed on output) works as expected
-e (execute following string as command-line) generates error message
    -- not emulated.  Works only under MPW Perl.
-0 (sets $\ in octal) works as expected
</pre>

<p>
Although in this context, I can't imagine you doing such a thing, I feel
the need to point out that:
</p>

<p>
-u (does core dump) Generates error message: &quot;Believe me, you don't
want to use &quot;-u&quot; on a Macintosh.&quot;
</p>

<h3>TMTOWTDI</h3>

<p>
In the course or preparing this article, Jim Correia proposed the following
&quot;reverse lines&quot; filter:
</p>

<pre>
#!perl -w
# Here is quickie example that reverses the selected lines.  If it looks
# like C it is because I write C all day long and it influences my perl

use strict;

while ( &lt;&gt; )
{
      my $s = $_;
 
      chomp($s);
 
      my $ct = length($s);
 
      for(my $i=$ct-1; $i&gt;=0; $i--)
      {
          print substr($s, $i, 1);
      }

      print "\n";
}
</pre>

<p>
One of the advantages of Perl is that "There's More Than One Way to 
Do It"; the C, AWK, or SED programmer can quickly complete and use a 
filter in the style with which they are most familiar.  But by taking 
advantage of Perl's advanced string handling and command line 
switches, one can accomplish same thing as the script above in one 
short line.
</p>

<p>
First, note that the entire script is encased in a <code>while()</code>.
This calls for either <code>-p</code> or <code>-n</code> on the hash-bang
to remove that construction.  Second, note that the <code>while()</code>
ends with a <code>print()</code>.  This makes it <code>-p</code> without a
doubt.
Third, note that the script <code>chomp()</code>s input and appends
&quot;\n&quot; to its output. Assigning  <code>$_</code> to <code>$s</code>
is an uncessary step. It's more idiomatically perlish to just
&quot;<code>chomp;</code>&quot;, which performs <code>chomp($_)</code> by
default, thus dropping <code>$s</code> from the namespace.  This calls for
<code>-l</code>, thus removing both <code>chomp()</code> and
<code>print()</code>.
</p>

<p>
Since the purpose of the <code>for()</code> loop is duplicated by the
built-in function <code>reverse()</code>, and reverse applies to
<code>$_</code> by default, the entire for loop becomes &quot;<code>$_ =
reverse</code>&quot;, and all the associated variables are dropped from the
namespace.  The final one liner is:
</p>

<pre>
     #!perl -wpl
     $_ = reverse;
</pre>

<p>
The idiomatic version is <b>trivially</b> faster and less memory
intensive. In practice, both versions are too fast to time.
</p>

---===---
David Steffen, Ph.D.
President, Biomedical Computing, Inc. <http://www.biomedcomp.com/>
Phone: (713) 610-9770 FAX: (713) 610-9769 E-mail: steffen@biomedcomp.com

==== Want to unsubscribe from this list?
==== Send mail with body "unsubscribe" to macperl-scribes-request@macperl.org