开发者

perl code behaving strange

I have written a code to get the url of a website and then search for a string and then compare that string(actually a number) with a hardcoded number

#!/usr/bin/perl

use LWP::Simple;

my  $oldversion =36开发者_Python百科;


$pageURL="http://www.google.com/isos/preFCS5.3/LATESTGOODCVP/";   
my $simplePage=get($pageURL);  


my $newPage = "$simplePage";
my $str = (split("href=\"CVP-LATEST-5.3.0.",$newPage ))[1];

my $version = substr("$str",0,2);

print $version;               // HERE IT PRINT 37 WHICH IS CORRECT


if($version =! $oldVersion )
{


print $version;               // BUT HERE IT PRINTS 1 WHICH IS WRONG. HOW IS IT CHANGING ?

##-- fetch the zip and save it as perlhowto.zip
my $status = getstore("http://www.google.com/isos/preFCS5.3/LATESTGOODCVP/CVP-LATEST-5.3.0.$version.iso", "CVP-LATEST-5.3.0.$version.iso");
}
else
{
print("Currently new version\n");
}

Why is it changing the value ? its not able to download the file becuase of that.


You mean !=, not =!, which is an assignment of a negation.

Also, split always uses a regex (except for the very special case of a string that has a single space), so those .s in 5.3.0. will match any non-newline. You probably want to \-escape them.

You may be interested in the uscan script in the debian devtools package.


You have got your "not equals" operator backwards. It should be != rather than =!.

By using =! you are in effect saying "set $version to the negated value of $oldversion".

Here is the offending line

if($version =! $oldVersion )  # Should be if($version != $oldVersion )

Also notice that by using the != operator you are telling perl that $version and $oldversion contain numbers. For string comparisons you should use the ne operator, which assumes that these variables contain strings.

if($version ne $oldVersion )  # String inequality

Here is the documentation for equality operators -

http://perldoc.perl.org/perlop.html#Equality-Operators


It's because you are assigning to $version the value !$oldVersion in this "test":

if($version =! $oldVersion )

And $oldVersion is nothing--but $oldversion is 37. You are assigning $version the boolean negation of an undefined variable. Undefined is boolean false, and so the negation is boolean true or 1.

If you read very much on perl, you're bound to come across the advice to use strict and warnings. Had you done that, it would have told you, among other things:

Global symbol "$oldVersion" requires explicit package name at - line 21.

This means that you didn't declare $oldVersion as lexical (my) or package-level (our) in this package, so if you want to use it, please include the package where you're getting it. In a vast majority of cases, a seasoned Perl programmer will recognize this as "Ugh, I didn't declare $oldVersion!" and the reason is that you declared $oldversion.


Your use of split doesn't make a lot of sense here. What you really want are the two digits following the CVP-LATEST-5.3.0. string. You're also not really doing anything by assigning one variable to another with the addition of quotes ($newPage = "$simplePage").

And, of course, as others have pointed out, the comparison is != not =!.

I'd rewrite this as:

use strict;
use warnings;
use LWP::Simple;

my $oldVersion = 36;
my $url        = 'http://www.google.com/isos/preFCS5.3/LATESTGOODCVP/';

my $newPage = get($url)
    or die "Cannot retrieve contents from $url\n";

if ( $newPage =~ /href=\"CVP-LATEST-5\.3\.0\.(\d\d)/ ) {
    my $version = $1;

    if ( $version != $oldVersion ) {
        my $status = getstore($url . "CVP-LATEST-5.3.0.$version.iso",
                              "CVP-LATEST-5.3.0.$version.iso");
    } else {
        print "Already at most recent version\n";
    }

} else {
    die "Cannot find version tag in contents from $url\n";
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜