[plug] Save a soul from scripting insanity [conclusion?]
Anthony J. Breeds-Taurima
tony at cantech.net.au
Thu Aug 15 09:51:48 WST 2002
On Wed, 14 Aug 2002 ryan at is.as.geeky.as wrote:
> 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
Ummm Why do you need the 'm' prefix? AFAIK
m{Translator\.translate\("((?:\\["\\]|[^"])*)"[^\)]*\)}g;
== /Translator\.translate\("((?:\\["\\]|[^"])*)"[^\)]*\)/g;
albeit slightly easier to read
> ================================
> #!/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";
> }
> ================================
Does it handle?
---
System.out.println(Translator.translate(
"String 1") +ojb.meth() +Translator.translate("String 2a"
+"\"String 2b;\"",27, 12),
Translator.translate("(String 3)"));
---
I tried very hard to come up with one (or even two regex's) that would work
and failed. If you've managed to do it then I need to take my hat off ;P.
As an aside:
---
open(INFILE,"<$file") or die "*** Can't open input file $file - $!";
my @lines = <INFILE>;
foreach my $line (@lines) {
$data .= $line;
}
---
Can be done in a single read
---
open(INFILE,"<$file") or die "*** Can't open input file $file - $!";
{
local($/) = undef
$data = <INFILE>;
}
close(INFILE);
---
Avoiding the large'ish memory memory requirements of the foreach.
Which you should also avoid in the outtermost loop.
Yours Tony
Jan 22-25 2003 Linux.Conf.AU http://linux.conf.au/
The Australian Linux Technical Conference!
More information about the plug
mailing list