Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: fixing use of

by AidanLee (Chaplain)
on Dec 18, 2001 at 01:17 UTC ( [id://132671]=note: print w/replies, xml ) Need Help??


in reply to Error message when using 'our'

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 perldoc.com.

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).

Replies are listed 'Best First'.
Re: Re: fixing use of
by wog (Curate) on Dec 18, 2001 at 01:24 UTC

    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.)

Re: Re: fixing use of
by clintp (Curate) on Dec 18, 2001 at 02:25 UTC
    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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://132671]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-03-29 12:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found