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

[MacPerl-Scribes] Computer Problems....



All--

If you're wondering, I've been having numerous problems with my Mac, mostly 
having to do with unresolved extension conflicts, but also a rather botched 
attempt to install a scanner (shipped to me by my busienss partner, without 
drivers (which are a 20Meg d/l from the UMAX website)).  My article on Perl 
Filters is next on the schedule, but I suspect that for most of the day 
TODAY (and I note that it's the 19th, and I'm behind schedule), I'll 
probably have my Mac torn apart on the kitchen table as I install 
additional RAM. (no fear, my actual technical degree is in electronics,  I 
just no longer make a living at it).  After that, I'll be installing MacOS 
9, and retiring my 14.4K modem.  Most of this is damage control, except for 
the MacOS 9 install, which is a LONG OVERDUE upgrade. I'm up and running 
this morning only by dint of uninstalling Quicktime (don't ask me WHY, but 
that seems to be PART of the extension conflict).

End result is I need help!  I can't work on the article while I've got my 
Mac torn apart.  Here's the 858 words I've written so far.  It trails off 
at the end, and needs a conclusion of some sort.  It also needs HTML 
formatting (some tags already applied).  Alas,  I'm gone from the 'net for 
the rest of the day, and won't be back until evening.  Hash it out, this is 
open-source journalism!  You may ask Jim Correia if he wants to contribute 
some details on how Perl Filters work, that could flesh things out a bit 
too.

Oh, and for the last 30 days, I've been using a demo copy of MailSmith, and 
unfortunately, forgot to transfer contact information I'd collected during 
that time.  If there's any reason you think I might need to have your email 
address, drop me a line.  I don't keep those things in my head (I have over 
600 people I actively correspond with (I could (and have) start(ed) a 
mailing list (several actually)).

<h2>Basic Perl Filters in BBEdit</h2>

Back in issue #7, Vicki Brown wrote about her "<a 
href='http://www.perlmonth.com/columns/mac_perl/mac_perl.html?issue=7'>MacPe
rl 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 <a 
href="http://www.barebones.com/>Bare Bones Software</a>.  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.  <a href="http://alpha.olm.net/">Alpha</a> 
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.

<h3>An Example One-Line Filter</h3>

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.  Frequently, 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 approached 
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 concise form.  Ron 
claims the result is again "ugly", but the original poster referred to it 
as "inspirational".

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

<h3>Filters using Perl Modules</h3>

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!

<h3>Filters with No Input</h3>

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, such as generating an 
XML file, then parsing it to create 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 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:

-n (iterate entire script using <>) works as expected
-p (iterate entire script using <> printing each line) worksas 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

Although in this 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.




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