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

Hello.
I'm afraid I'm rather new to perl, and my understanding of it so far has been based upon a 'trial-and-error' approach, so please bear with me.

I'm trying to execute a script to extract an openstreetmap planet.osm file into a postgreSQL databse. The script calls on several modules, which I have so far managed to install without too much difficulty. However, I have stumbled across an error that I am unsure how to solve.

The error in this case relates to the planet.pm module, which uses the 'strict' pragma. When executing planetosm-to-db.pl the following error is returned:

Global symbol "$VERBOSE" requires explicit package name at /System/Lib +rary/Perl/5.8.6/darwin-thread-multi-2level/Geo/OSM/Planet.pm line 165 +. Global symbol "$DEBUG" requires explicit package name at /System/Libra +ry/Perl/5.8.6/darwin-thread-multi-2level/Geo/OSM/Planet.pm line 165.
Line 165 reads:

print STDERR "Mirror OSM Data from $url\n" if $VERBOSE || $DEBUG;

which I altered to

print STDERR "Mirror OSM Data from $url\n" if my($VERBOSE) || my($DEBUG);

resulting in more explicit package name errors – on lines 175, 180, 195, 210, 237, 241, 243, 245, 259, 274, 281. Editing these lines in a similar manner returns the following error on lines 241, 243, 245, 259:
"my" variable $DEBUG masks earlier declaration in same scope at /Syste +m/Library/Perl/5.8.6/darwin-thread-multi-2level/Geo/OSM/Planet.pm lin +e 241. "my" variable $VERBOSE masks earlier declaration in same scope at /Sys +tem/Library/Perl/5.8.6/darwin-thread-multi-2level/Geo/OSM/Planet.pm l +ine 241.
I understand that I have too many instances of "my" but am not sure why, and therefore unsure how to proceed in order to get the module to run when I execute the script. Any explanation of what I have done wrong – and why it isn't working at present – would be very helpful. Thanks.

Replies are listed 'Best First'.
Re: "my" variables masking earlier declaration
by talexb (Chancellor) on Nov 06, 2007 at 17:10 UTC

    The my keyword declares variables. You have to declare a variable before you use it (under use strict), otherwise you'll get the Global symbol "$VERBOSE" requires explicit package name at .. type of message. But you can't re-declare a variable, otherwise you'll get the "my" variable $DEBUG masks earlier declaration in same scope .. type of message.

    Define a variable just once, and before it's needed, then Perl won't (or shouldn't) complain.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Re: "my" variables masking earlier declaration
by ikegami (Patriarch) on Nov 06, 2007 at 17:09 UTC

    my creates a new variable. That's really not what you want. Presumably, $VERBOSE and $DEBUG are package variables that can be declared using use vars qw( $VERBOSE $DEBUG );

Re: "my" variables masking earlier declaration
by Anonymous Monk on Nov 06, 2007 at 18:07 UTC
      Thanks for the links to the tutorials – I think I now understand what I was doing wrong, and hence why the script wasn't working.
Re: "my" variables masking earlier declaration
by GrandFather (Saint) on Nov 06, 2007 at 21:41 UTC

    Note that we generally strongly recommend that you use strictures (strict and warnings) at the start of all your Perl scripts. They give early warnings of impending doom and are especially valuable when you are new to Perl.


    Perl is environmentally friendly - it saves trees