Question Regarding Perl Mapscript

Trond Michelsen trondmm-mapserver at CRUSADERS.NO
Tue Sep 12 23:49:01 PDT 2006


On Tue, Sep 12, 2006 at 06:23:42PM -0400, Gabriel A Weaver wrote:
> Hi all,
> I am trying to install mapscript for perl and run into the following error:
> 
> [terminal: ]$ perl -e 'use lib 
> "/web/cgi-bin/Progs/lib/5.6.0/i386-linux"; use mapscript; my $map = new 
> mapObj("DBs/MapData/world.map"); if ($map == null) { print "UTOH!";} 
> else { print "IT WORKS!";}'

> Unfortunately, I keep getting "UTOH", indicating I have a null pointer 
> instead of a mapObj object.

Actually, in this case it indicates that your perl-code is wrong. 

perl -le 'if ("string" == null) {print "EGAD!"} else {print "Whohoo!"}'

will print "EGAD!"

The problem is that null means nothing in perl (erm - what I mean is -
it has no special meaning in perl :).  And since you're not using
strict or warnings, that means that null is promoted to a string. But
"string" does still not equal "null", you might think. That's true,
but you're not checking if they're equal, you're checking if they're
_numerically_ equal. In perl, strings that does not look like a number
has the numerical value of 0, so in this case, "string" is indeed
numerically equal to "null".

When you program perl, you should ALWAYS! use strict and use
warnings. This will catch a lot of this type of mistakes. In this
case, strict would have resulted in a compilation error:

$ perl -Mstrict -le 'if ("string" == null) {print "EGAD!"} else {print "Whohoo!"}'
Bareword "null" not allowed while "strict subs" in use at -e line 1.
Execution of -e aborted due to compilation errors.

While just using warnings would have given these warnings:
$ perl -Mwarnings -le 'if ("string" == null) {print "EGAD!"} else {print "Whohoo!"}'
Unquoted string "null" may clash with future reserved word at -e line 1.
Argument "null" isn't numeric in numeric eq (==) at -e line 1.
Argument "string" isn't numeric in numeric eq (==) at -e line 1.
EGAD!

You're looking for undef. Also, the best way to test for definedness,
is to use the defined() function.

if (defined($map)) {...}

You could also check if $map is a reference

if (ref($map)) {...}

I haven't looked at the mapscript documentation in a while, so I'm not
sure what mapObj->new() will return in case of a failure, though.

-- 
Trond Michelsen



More information about the MapServer-users mailing list