problem with simple perl Regular Expression match - Perl
This is a discussion on problem with simple perl Regular Expression match - Perl ; When I run this script,
$result = `$WGET $file`;
print $result . "\n";
if ( $result =~ m/saved/ ){
print "MATCH\n";
} else {
print "NO MATCH\n";
}
It outputs,
2008-11-11 07:43:34 (526 KB/s) - `test.zip' saved [1517245/1517245]
NO MATCH
...
-
problem with simple perl Regular Expression match
When I run this script,
$result = `$WGET $file`;
print $result . "\n";
if ( $result =~ m/saved/ ){
print "MATCH\n";
} else {
print "NO MATCH\n";
}
It outputs,
2008-11-11 07:43:34 (526 KB/s) - `test.zip' saved [1517245/1517245]
NO MATCH
But if I change,
- $result = `$WGET $file`;
+ $result = "2008-11-11 07:43:34 (526 KB/s) - `test.zip' saved
[1517245/1517245]";
the script now outputs
2008-11-11 07:43:34 (526 KB/s) - `test.zip' saved [1517245/1517245]
MATCH
Why doesn't the match,
$result =~ m/saved/
work in the first case?
--JC
-
Re: problem with simple perl Regular Expression match
Hi Rob,
On Tue, Nov 11, 2008 at 8:14 AM, Rob Coops <rcoops@gmail.com> wrote:
> I am not 100% certain but I would say because in the first instance $WGET
> $file does not result in anything, the printed line is not printed by your
> program but by the wget command.
>
> Try modifying your script to the follwoing:
> $result = `$WGET $file`;
> print "Result = $result\n";
You're correct.
That now outputs
2008-11-11 08:16:55 (550 KB/s) - `test.zip' saved [1517245/1517245]
Result =
How *can* I correctly capture that WGET output into a variable that
can be then tested?
--JC
-
Re: problem with simple perl Regular Expression match
I stumbled on an answer. Adding "2>&1" apparently works, i.e.,
$result = `2>&1 $WGET $file`;
Not sure if it's the only/best way.
--JC
-
Re: problem with simple perl Regular Expression match
On Tue, Nov 11, 2008 at 11:38, JC Janos <jcjanos245@gmail.com> wrote:
> I stumbled on an answer. Adding "2>&1" apparently works, i.e.,
>
> $result = `2>&1 $WGET $file`;
>
> Not sure if it's the only/best way.
snip
Well, the best way would be to use LWP*, LWP::Simple**, or
WWW::Mechanize*** to allow Perl to get the data without resorting to
an external program:
#!/usr/bin/perl
use strict;
use warnings;
use LWP::Simple;
use File::Basename;
my $url = "http://wonkden.net/resume.pdf";
my $file = basename $url;
my $status = mirror($url, $file);
if ($status == RC_OK) {
print "got file\n";
} elsif ($status == RC_NOT_MODIFIED) {
print "didn't need to get the file\n";
} else {
print "got error $status\n";
}
* http://search.cpan.org/dist/libwww-perl/lib/LWP.pm
** http://search.cpan.org/dist/libwww-p.../LWP/Simple.pm
*** http://search.cpan.org/dist/WWW-Mech...W/Mechanize.pm
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
-
Re: problem with simple perl Regular Expression match
Chas,
> my $status = mirror($url, $file);
>
> if ($status == RC_OK) {
> print "got file\n";
> } elsif ($status == RC_NOT_MODIFIED) {
> print "didn't need to get the file\n";
That's a good example, thanks. LWP looks like a good alternative; one
question ...
As I use "wget --timestamping ...", it doesn't DL the file unless the
timestamp is different.
Is LWP::Simple's mirror() function similar to wget's "--mirror"
option, in that it supports/uses timestamping?
Looking at your example I see
"didn't need to get the file"
does that mean that it DID retrieve the file, and simply determined
that it wasn't necessary? Or that it 1st determined that it wasn't
necessary and didn't even DL the file?
--JC
-
Re: problem with simple perl Regular Expression match
On Tue, Nov 11, 2008 at 15:38, JC Janos <jcjanos245@gmail.com> wrote:
> Chas,
>
>> my $status = mirror($url, $file);
>>
>> if ($status == RC_OK) {
>> print "got file\n";
>> } elsif ($status == RC_NOT_MODIFIED) {
>> print "didn't need to get the file\n";
>
> That's a good example, thanks. LWP looks like a good alternative; one
> question ...
>
> As I use "wget --timestamping ...", it doesn't DL the file unless the
> timestamp is different.
>
> Is LWP::Simple's mirror() function similar to wget's "--mirror"
> option, in that it supports/uses timestamping?
>
> Looking at your example I see
>
> "didn't need to get the file"
>
> does that mean that it DID retrieve the file, and simply determined
> that it wasn't necessary? Or that it 1st determined that it wasn't
> necessary and didn't even DL the file?
>
> --JC
>
From http://search.cpan.org/dist/libwww-p...mple.pm#mirror
mirror($url, $file)
Get and store a document identified by a URL, using
If-modified-since, and checking the Content-Length. Returns the HTTP
response code.
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
-
Re: problem with simple perl Regular Expression match
On Tue, Nov 11, 2008 at 4:23 PM, Chas. Owens <chas.owens@gmail.com> wrote:
> From http://search.cpan.org/dist/libwww-p...mple.pm#mirror
Sure. I've read the manual. I asked my question because the manual
wasn't clear to me. It still isn't.
But anyway, I figured out what happens by trial & error.
Thanks.
--JC