Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Help needed understanding global variables in Perl

by steves (Curate)
on Mar 05, 2002 at 14:26 UTC ( [id://149374]=note: print w/replies, xml ) Need Help??


in reply to Help needed understanding global variables in Perl

This is a good discussion because it brought out some subtle differences between our and globals declared without a scoping qualifier (and use vars ...) that I hadn't thought about. I've actually only been using 5.6 for a while now so our is a bit new to me.

Here's my question: I was looking at our as a way to declare a global package variable and access that variable unqualified from its package while using strict. But if our also implies scoping where regular globals don't, are there other differences? The big question here would relate to global package variables accessed by other packages. I'm thinking of things like $VERSION and @ISA. Is there a difference declaring these with our versus declaring them unqualified with an associated use vars? our scoping rules seem to indicate that declaring these at the top of a file (compilation unit) limits their scope to that compilation unit. If so, what does that mean for variables accessed by other packages, like @ISA?

  • Comment on Re: Help needed understanding global variables in Perl

Replies are listed 'Best First'.
Re: Re: Help needed understanding global variables in Perl
by erikharrison (Deacon) on Mar 05, 2002 at 15:10 UTC

    I do not claim to have plumbed the depths of our. However, this seems to be the deal: 'our' variables are package variables, just like $main::foo, except that our makes a LEXICALLY scoped declaration that the variable has been legally imported - basically, you can use it's unqualified package name under use strict.

    use strict; package foo; our $y = "This is in package foo"; # $x and $y both package $foo::x = "This is in package foo"; # variables print $foo::x; { #bare block our $x; print $x; #$x = $foo::x } #end of bare block print $x #Give a warning under use strict #because the 'our' declaration #is out of scope package bar; print $foo::y; #prints $y declared with 'our' in package foo print $foo::x; #prints $x declared with package name in foo

    I don't know if that helps any, (or just confused) but there it is - the weirdness (and, I think, good use) of our.

    Cheers,
    Erik
Re: Re: Help needed understanding global variables in Perl
by gellyfish (Monsignor) on Mar 05, 2002 at 15:03 UTC

    I'm thinking of things like $VERSION and @ISA.

    These kind of things are fine, you are only declaring them with our for your own convenience within your module - when they are accessed by perl itself or by another module (in the case of $VERSION and ExtUtils::MakeMaker for instance,) they are accessed as if they were fully qualified. Infact h2xs does declare them as our by default :

    our @ISA = qw(Exporter); # .. removed the comments our %EXPORT_TAGS = ( 'all' => [ qw( ) ] ); our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); our @EXPORT = qw( ); our $VERSION = '0.01';

    /J\

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-20 02:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found