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

Yo Monks!

I've got a bit of code that I have to move to a new machine. On the new machine, when it runs, it gives the following error message:

Can't call method "our" on an undefined value at line 61.

line 58-63:

## fills in the template - in this case it will likely be a form sub assigntags { my @tags = @_; our $show_fields; my %tagvals = (); foreach $tag (@tags) { ...

The "our $show_fields;" is the problem.

The version of perl that the code was developed with and runs on is 5.6.1 for sun4-solaris. The destination machine has perl version 5.005_03 for sun4-solaris.

My question is this: I have very little understanding of scoping and the (deprecated) use of the our keyword, but I need to get this code runnin on 5.005_03. Can anyone offer some explanation and advice?



Replies are listed 'Best First'.
Re: fixing use of our in perl 5.0*
by wog (Curate) on Dec 18, 2001 at 01:15 UTC

    our is not deprecated. It's too new.

    our acts like use vars (which is less recent) except that it is lexically scoped in effect like my. our was introduced in perl version 5.6.0

    You should be able to replace the use of our with use vars..

Re: fixing use of
by AidanLee (Chaplain) on Dec 18, 2001 at 01:17 UTC

    fairly simple. our is reserved, but not used, in 5.005_03. from perldelta for 5.005_03:

    our is now a reserved word

    Calling a subroutine with the name our will now provoke a warning when using the -w switch.

    Found this info via

    I don't know the reason your script used our so my suggestion for fixing it (change it to a use vars statement) may not be appropriate.

    Update: In addition to wog's note below, I've also realized that I missed how the statement was used in the example code. Why are you declaring 'our' inside a subroutine? I don't know what the style trend is in the general community, but if I'm defining a package variable (which 'our' does) then I wouldn't do it from within a subroutine.

    If the variable is used solely within the sub, use my. Otherwise, use vars and I would recommend taking the declaration out of the subroutine (personal preference).

      our is not in this case not be recognized as a subroutine call. The error indicates it's being used as a method call (in the indirect object syntax), but the value it's being called on, $show_fields, is not defined, and hence perl can't lookup the method to call. I would note that you'd probably should get an error from that suggests that it was being redeclared for some reason or that strict is not being used. (Since strict would give a different error on the use of an undeclared variable.)

      Not to nitpick, but thought you might find this interesting:
      package main; my $var=6; sub foo { our $var; $var=5; } print $var; foo(); print $main::var; print $var;
      This prints "656".

      What is means is, that within the subroutine foo, I can get to the package $var without having to use a full package name ($main::var). The our declaration makes it clear that within this lexical scope "$var" means the package variable, not the lexical $var that exists in the enclosing scope.

      Sometimes there are uses for "our" inside of subroutines.

      update: of course I meant $main::var