[plug] Save a soul from scripting insanity [conclusion?]

ryan at is.as.geeky.as ryan at is.as.geeky.as
Wed Aug 14 18:45:24 WST 2002


For those that are following the story *sees no hands going up* ...

Once Ryan stopped typing and _thought_ about the problem and realised he
knew of the Perl 'm' regex prefix, he thus compressed the entire program to
this:

================================
#!/usr/bin/perl

use strict;
use File::Find;

$" = "\n";
my @files;

find ( sub { if (-e $_) { push @files, $File::Find::name; } }, $ARGV[0]);

foreach my $file (@files) {
    my (@found, $data);
    open(INFILE,"<$file") or die "*** Can't open input file $file - $!";
    my @lines = <INFILE>;
    foreach my $line (@lines) {
        $data .= $line;
    }
    $data =~ s/\"\s*\+\s*\n?\s*\"//g;
    @found = $data =~
m{Translator\.translate\("((?:\\["\\]|[^"])*)"[^\)]*\)}g;
    print "@found";
}
================================

It will handle a 'complex' example such as this:

  a.print(Translator.translate("line 1" +
     " -- mid " +
     " -- line 1"), 232, Translator.translate("line 2" +
     " -- line 2"), Translator.translate("line 3", 123, 1234),
b.print(Translator.translate("line 4"))); Translator.translate(,12);
Translator.translate("",123);

Returns:
=====================
line 1 -- mid  -- line 1
line 2 -- line 2
line 3
line 4

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

Did it work Russell?

Ryan



More information about the plug mailing list