T
Tore Aursand
Hi!
I have a number of old Perl scripts doing fairly the same job; They're
connecting to a (local) web server and retrieves something from it (plain
text, that is).
As I said, each script is doing the same job; Parsing the text, and
writing some "meaningful" data to a MySQL database.
Everything works just great, but I want to put everything these scripts do
into one script, as most of what they do is identical. I have created a
list of hashes which describe each resource I try to parse;
my %sources = (
{
'title' => 'Server #1',
'href' => 'http://.../1/',
'regexp' => '...',
},
{
'title' => 'Server #2',
'href' => 'http://.../1/',
'regexp' => '...',
},
# etc...
);
Iterating through these resources;
foreach ( @sources ) {
my $text = get( $_->{'href'} ); # LWP::Simple
if ( defined $text && length $text ) {
while ( $text =~ m,$_->{regexp},sig ) {
my $foo = $1;
my $bar = $2;
# etc...
}
}
}
This works as expected for the majority of the files I download, but for
some I need to - hmm - match in a different order. Example: For most of
the sites it is suitable to set $foo = $1, but for some $foo should be $2
instead (or $3, whatever).
How should I deal with this in a sexy way?
--
Tore Aursand <[email protected]>
"Writing is a lot like sex. At first you do it because you like it.
Then you find yourself doing it for a few close friends and people you
like. But if you're any good at all, you end up doing it for money."
(Unknown)
I have a number of old Perl scripts doing fairly the same job; They're
connecting to a (local) web server and retrieves something from it (plain
text, that is).
As I said, each script is doing the same job; Parsing the text, and
writing some "meaningful" data to a MySQL database.
Everything works just great, but I want to put everything these scripts do
into one script, as most of what they do is identical. I have created a
list of hashes which describe each resource I try to parse;
my %sources = (
{
'title' => 'Server #1',
'href' => 'http://.../1/',
'regexp' => '...',
},
{
'title' => 'Server #2',
'href' => 'http://.../1/',
'regexp' => '...',
},
# etc...
);
Iterating through these resources;
foreach ( @sources ) {
my $text = get( $_->{'href'} ); # LWP::Simple
if ( defined $text && length $text ) {
while ( $text =~ m,$_->{regexp},sig ) {
my $foo = $1;
my $bar = $2;
# etc...
}
}
}
This works as expected for the majority of the files I download, but for
some I need to - hmm - match in a different order. Example: For most of
the sites it is suitable to set $foo = $1, but for some $foo should be $2
instead (or $3, whatever).
How should I deal with this in a sexy way?
--
Tore Aursand <[email protected]>
"Writing is a lot like sex. At first you do it because you like it.
Then you find yourself doing it for a few close friends and people you
like. But if you're any good at all, you end up doing it for money."
(Unknown)