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

Hello Wise Monks,

Please see the code below and let me know what I am doing wrong here.

sub values; @X; use strict; # P is a package which has some subroutines and variables that I need +to use in my script. use P qw( $CurDir $a2 &guest &guest2 ); my $test = "TEST"; my $testdir = "$CurDir/$test"; # QUESTION: Do I need to use 'my' for these variables? # ERROR: If I don't use 'my' I get the error saying that $test require +s explicit package name. # If I don't use 'my' don't they automatically become global? Are they + not available for use? &values(); &guest(); &guest2( @X, "$test"); # QUESTION: I get an error saying that @X requires explicit package na +me. # How are global variable defined in a perlscript and how can they be +used, for the stuff I am doing in this script? sub values{ @X = ( something something ); # QUESTION: Same error as above! Need Explicit Package name. }

What am I doing here? Why do I keep getting the errors as mentioned above. Don't these variables come under the main Package?

What can I do to resolve these conflicts?

Thank you for your wisdom.

A Learning Monk!

Replies are listed 'Best First'.
Re: What am I doing here?
by iakobski (Pilgrim) on Dec 05, 2001 at 14:05 UTC
    You have use strict at the top of your script. This is a good thing - in fact it is an excellent thing: it will catch many common mistakes. One thing it will stop you doing is using globals. This is another good thing, so I won't tell you how to circumvent it - instead you should look at why you are using a global and see if there is a better way of doing it. You are passing the variable to the function in the other package so you might not need it to be global. If you want to share the variable between packages look up the our keyword in your documentation.

    -- iakobski

Re: What am I doing here?
by MZSanford (Curate) on Dec 05, 2001 at 14:39 UTC
    I must say that strict is a good thing, no doubt. You need to look at the my function. my will let you define a scope for your variables. This means that the varaible will not be visable to the code outside the current block (beit a sub, main::, etc ...) Something like :
    sub values{ my @X = ("something","something"); return(@X); }

    Then, instead of treating @X as a global, it is returned by &values(). Think of it this way, if someone saw the code, and not the subroutines, could they figure out where @X came from ? If the answer is no, then you probably have a global. So, using the values sub above, you could say :
    my @vals = &values(); &guest(@vals); ## i assume it uses this ? maybe not &giest2( @vals, 'test');

    Hope that gets you started on the right path ...
    $ perl -e 'do() || ! do() ;' Undefined subroutine &main::try
Re: What am I doing here?
by andye (Curate) on Dec 05, 2001 at 15:42 UTC
    Hi ginju75 - Even under use strict (which doesn't normally allow globals), you can declare globals with use vars qw($foo %bar) . Alternatively, you can 'switch off' this aspect of strict, within a limited scope, using no strict "vars".

    But looking at your code, it doesn't look as though @X needs to be global - I'd rewrite it like this:

    use strict; my $test = 'test'; my @X = values(); guest2(\@X, $test); sub values ( my @Y = qw(something something); return @Y; ) sub guest2 ( my @X = @{shift()}; # these copies of @X and $test my $test = shift ; # don't exist outside guest2() }
    Have a read of perlman:lib:strict and perlman:perlsub.

    hth,
    andy.

      Hi,

      good point about the use vars. In perl 56x there is also the our commando to define a global var. But, there was much discussion about this our, for example this and this.

      Just my 0.02 cents worth.
      ---------------------------
      Dr. Mark Ceulemans
      Senior Consultant
      IT Masters, Belgium