SPUG: Confusing behaivior with exported variables

Mark Mertel mark.mertel at yahoo.com
Tue Mar 24 12:25:39 PDT 2009


________________________________
From: Tye McQueen <tyemq at cpan.org>
To: Mark Mertel <mark.mertel at yahoo.com>
Cc: Peter Darley <pdarley at kinesis-cem.com>; spug-list at pm.org
Sent: Monday, March 23, 2009 6:06:33 PM
Subject: Re: SPUG: Confusing behaivior with exported variables


On Mon, Mar 23, 2009 at 3:24 PM, Mark Mertel <mark.mertel at yahoo.com> wrote:

Globals are not recommended. You could make them package variables and declare them with 'my' or 'our', and then access them using the package name $Neo::Web::Session, or $Darley::Supers::CurrentUser when from outside the package.

You are muddling several different things together there.

Complaining about "globals" and then recommending "our" is contradictory since "our" is used to declare a global variable.

Using 'our' is only global within the package and its descendents. For the external scripts it would not be visible, hence, not global.

Recommending "my" along with "using the package name" is bad advice since it won't work (you would be accessing different variables).

It does work:

package Foo;
my $bar = "foo bar\n";
1;
...
 
use Foo;
use strict;
 
print $Foo::bar; # prints 'foo bar'
 
print $bar; # should throw an error


I'll leave the in-depth explaining of the differences between "lexical variables" and "package globals" to other sources, but those are the two choices. :)

Tye



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/spug-list/attachments/20090324/c3fe5e58/attachment.html>


More information about the spug-list mailing list