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

[MacPerl-Scribes] Perl Filters article (rough draft)



Very rough!  I spent a WEEK away from home when I should have been writing this. 
My mother's been suffering double-vision as a result of a minor stroke, so I've
been helping her with her iMac, and struggling with the lack of my familiar
tool-kit.  Finally get home to an inbox with 300 messages in it, and had to dig
out.  This is going to need a lot of work in a VERY short time.

Missing even STUBS of references, far from complete, etc.  VERY ROUGH draft,
folks.  Feel free to pick it apart.

=============

Basic Perl Filters in BBEdit

Back in issue #7, Vicki Brown wrote about her "<a
href='http://www.perlmonth.com/columns/mac_perl/mac_perl.html?issue=7'>MacPerl
Development Environment</a>".  For the most part, what she describes there is a
typical setup for a serious MacPerler.  In her article Vicki covered, among
other things, BBEdit, from Bare Bones Software.  As of version 5.1, BBEdit has
MacPerl support built in.  Older versions used a plug-in module for roughly the
same functionality  However having built-in support means MORE than just the
ability to use BBEdit as a text-editor and front end for MacPerl.

BBEdit allows you to create "Perl Filters"; scripts to which a text selection,
or even an entire document may be passed, which then return their results to
BBEdit. BBEdit isn't the only Mac text-editor with built-in Perl support, and
neither is it the only editor which lets you use Perl scripts as text filters. 
Alpha has similar functionality, and a very vocal following.  I'm hoping to
corral one of those vocal followers into writing a column on the subject.

An Example One-Line Filter

For the most part, Perl Filters tend to be short one or two line scripts, which
do various regular expression functions not possible with BBEdit's own regex
capability.  Often times you need to make multiple passes over a document, or
parse it in a way which isn't possible with regex alone, like so:

#!perl -p 
s/(\d+)-(\d+)/$1 > $2 ? "$1-$2" : join ', ', $1 .. $2/ge

Alas, I'm not the author.  This came about as the result of one of the frequent
filter-generation sessions on the BBEdit-Talk[1] list.  On a Friday, someone
blithely asked for a script which could parse a certain set of strictly
formatted strings of numbers.  The problem was first approched using a rather
convoluted AppleScript, which, even the author concluded was ugly.  Next, a
Perler tried his hand.  Finally, Ronald Kimball stepped in, reducing the Perl
script to its most idiomatically consice form.  Ron claims the result is again
"ugly", but the original poster refered to it as "inspirational".

Oh, and what does it do?  The answer is <a href="">here</a>.

Filters using Perl Modules

Are you lazy?  You just don't want to open your mail client to post mail?  You'd
rather stay in your text-editor?  Well, so did Rory Campbell-Lange:

#!perl -w
# campbell-lange@easynet.co.uk (Rory Campbell-Lange)
# only barely modified from the example in
# the Mail::Send POD

require Mail::Send;

@allmail = <>;
$to = shift @allmail;
chomp $to;
$subject = shift @allmail;
chomp $subject;
$msg = new Mail::Send;

$msg->to($to);
$msg->subject($subject);

$fh = $msg->open;
print $fh "@allmail";
$fh->close;

Oh, but that doesn't handle MIME attachments you say?

#!perl -w
# campbell-lange@easynet.co.uk (Rory Campbell-Lange)
# only barely modified from the example in
# the MIME::Lite POD

use MIME::Lite;
MIME::Lite->send('smtp', "mymail.example.org", Timeout=>200);

    my $to = shift;
    my $sub = shift;
    my $filetoattach = shift;
    my $filename = shift;
    my @message = @_;

  $msg = new MIME::Lite
    From     =>'me@mymail.co.uk',
    To       => $to,
    #Cc      => couldbeshift,
    Subject  => $sub,
    Type     =>'TEXT',
    Data     => @message,
    
  attach $msg
    Type     =>'image/gif',
    Path     => $filetoattch,

    Encoding => 'base64',
    Filename => $filename;

$msg->send;

A sufficiently intrepid programmer could easily use this as the starting point
for a complete mail client, written as a BBEdit Perl Filter!

Filters with No Input

Nothing says that a "filter" has to take an input to produce an output.  This
example creates a timestamp:

#!perl
    my ( $year, $month, $day, $hour, $min, $sec) = reverse((localtime)[0 .. 5]);
    printf('%04d:%02d:%02d 
%02d:%02d:%02d',$year+1900,$month+1,$day,$hour,$min,$sec);

I've done other scripts which do more complex things, like generating an XML
file, then parsing it to HTML.  However, I'm saving the really tricky Perl
Filters for Chris Nandor.

Finally, a word about switches.  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 seperate column, but I'll try to deal with it breifly
here.  Most switches work with MacPerl.  Of those which might be considered most
useful in the context of Perl Filters:

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

Although in ths context, I can't imagine you doing such a thing, I feel the need
to point out that:

-u (does core dump) Believe me, you don't want to use "-u" on a Macintosh.

===================

--B

Brian McNett, Webmaster/MacPerl Guru
*************************************************************
Mycoinfo. The world's first mycology e-journal.
http://www.mycoinfo.com/
*************************************************************



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