Hi Kathy,
> I'm very new at this. I know some Perl and barely any AppleScript. I would
> like to improve an AppleScript that parses a large text file ( > 500k) and
> re-writes it to another file so that it can be imported into a FileMaker
> database. I would like to call a perl script to do the parsing part. Do you
> think perl will be faster than applescript at parsing large files?
In a word, "yes".
> The current
> applescript is not using regular expressions. I understand that you can use
> them in applescript.
You can if you have the "RegEx Commands" osax in your scripting additions
folder. Much neater if you use the regexes built into perl however.
> I don't know how much to ask Perl to do. Should I
> open/create/move files in the AppleScript and let Perl do the text work? Or
> should I have Perl do everything? What is your advice about this project?
>From what you've written, virtually the whole hog will parse through the eye
of the camel. If you've got the Filemaker import piece working already I'd
throw that into an applescript and just feed it set of files that perl
creates. I've included below an attempt at some skeleton code to get you
started. Hope it's helpful.
Alternatively, as Chris suggested, there's the MacPerl::DoAppleScript()
command within MacPerl which could be placed after the call to
process_file; just use a variable containing the string version of the
applescript as its argument. Best done using a "heredoc" as per "MacPerl:
Power and Ease" page 178. The book is viewable as html if you haven't yet
purchased the paper version: the section I'm pointing to is at the bottom of
http://macperl.com/macperl/ptf_book/r/MP/315.MP_Pkg.html
Cheers,
Paul
--------------------------------------------------------------------------
#!/usr/bin/perl -w
use strict;
my $parsedir="Macintosh HD:toBeParsed:";
my $donedir="Macintosh HD:finishedParsed:";
opendir PARSE,$parsedir or die "opening parse dir: $!";
# assuming we want to go through everything in this directory
# and that there it contains no directories...
foreach my $filename (readdir PARSE){
process_file($filename);
}
sub process_file {
my $current = shift;
open OUT,">$parsedir${current}+parsed" or die "opening output file: $!";
open IN,"$parsedir$current" or die "opening input file: $!";
my $end_of_record=0; #see below
while (<IN>){
my $record="";
# do what's necessary here to construct a record, $record say, from as
# many input lines as are necessary: set the flag $end_of_record to 1
# to signal "print time". Then...
if ($end_of_record){
print OUT $record;
$end_of_record=0;
}
}
close OUT or die "couldn't close outfile: $!";
rename("$parsedir$current","$donedir$current") or die "rename problem:$!";
}
--------------------------------------------------------------------------