semio has asked for the wisdom of the Perl Monks concerning the following question:

fellow monks,

I've noticed an issue running a script on different platforms. The following script, with perl 5.8.8 built for i386-freebsd-64int, runs without issue.

use strict; use XML::Simple qw(:strict); use LWP::UserAgent; use Data::Dumper; use POSIX qw(strftime); getNVD(); sub getNVD { my $ua = LWP::UserAgent->new; $ua->agent( "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/200 +70309 Firefox/2.0.0.3" ); my $req = HTTP::Request->new( GET => 'http://nvd.nist.gov/download/nvdcve-recent.xml' ); $req->content_type('application/x-www-form-urlencoded'); my $res = $ua->request($req); my $fetch; if ( $res->is_success ) { $fetch = $res->content; } else { print $res->status_line; } my $today = strftime( "%Y-%m-%d", localtime ); my $yesterday = strftime( "%Y-%m-%d", localtime( time - 86400 ) ); my $xml = new XML::Simple; my $data = $xml->XMLin( "$fetch", KeyAttr => { cve => 'entry' }, ForceArray => [ 'cve', 'ref' ] ); my $e; foreach $e ( @{ $data->{entry} } ) { if ( $e->{severity} =~ /High/ && $e->{modified} =~ /($today|$yesterday)/ ) { print $e->{name}; } } #print Data::Dumper->Dump([\$data]); }
However the same code, when run on perl 5.8.8 built for MSWin32-x86-multi-thread, produces the following error:
Use of uninitialized value in pattern match (m//) at test.pl line 45.
There appears to be an issue with the following section:
if ( $e->{severity} =~ /High/ && $e->{modified} =~ /($today|$yesterday)/ ) {
Removing the references to $e->{severity} and $e->{modified} produce the same result. I should also note that the data across each platform when using Data::Dumper appears to be identical. Any thoughts on what may be causing this error?

cheers.

Replies are listed 'Best First'.
Re: possible cross platform issue
by tirwhan (Abbot) on Jul 12, 2007 at 15:05 UTC

    That isn't an error, it's a warning. Are you sure the script on Windows does not have "use warnings" or the -w switch on the shebang line?

    As for why the warning is being thrown, I would guess that either $e->{severity} and/or $e->{modified} are unset. Try printing out $e with Data::Dumper immediately before each match to confirm that or rule it out.


    All dogma is stupid.
Re: possible cross platform issue
by dsheroh (Monsignor) on Jul 12, 2007 at 16:16 UTC
    Assuming it's OK for severity/modified to be blank, you can avoid the warning by changing that to
    if ( $e->{severity} || '' =~ /High/ && $e->{modified} || '' =~ /($today|$yesterday)/ ) {
    or
    if ( defined $e->{severity} && defined $e->{modified} && $e->{severity} =~ /High/ && $e->{modified} =~ /($today|$yesterday)/ ) {

    On the other hand, you might need to look at the source of your data and see about making a change there to ensure that severity and modified are set on all records if they're supposed to be required.

    Either way, I would recommend making the behaviour consistent by adding use warnings right after use strict, so that they'll be on in both OSes, not by turning them off in Windows.