SPUG: Confusing behaivior with exported variables

Mark Mertel mark.mertel at yahoo.com
Tue Mar 24 16:06:22 PDT 2009


apologies - i'll abstain from further coding examples until i am able to compile them. i have no computer at present, and am attempting to get it from memory - kind of a pseudo coder. i did read up a little on the break, and i think my opinion about this was to not use global variables, but if they couldn't be avoided, use package variables instead. 

again, my apologies to the list.
 ---
Mark Mertel
206.441.4663
mark.mertel at yahoo.com
http://www.linkedin.com/in/markmertel
http://www.seattlejobs.com/13147468 




________________________________
From: Ronald J Kimball <rjk-spug at tamias.net>
To: spug-list at pm.org
Sent: Tuesday, March 24, 2009 1:37:33 PM
Subject: Re: SPUG: Confusing behaivior with exported variables

On Tue, Mar 24, 2009 at 12:25:39PM -0700, Mark Mertel wrote:
>
> 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

Did you actually execute this code?  It /does not/ do what you say it does.

It would be a very good idea for you to take the time to understand how
my()/our() and lexical/package variables actually work before you spread
more misinformation on this subject.

Variables declared with my() are lexically scoped.  They are not stored in
the symbol table.  They are accessible only from within that lexical scope.

Variables declared with our() are package variables.  They are stored in
the symbol table.  They are accessible globally, but may need to be fully
qualified.

package Foo;
use strict;

{
  my $foo = 'foo';  # sets lexical $foo

  print "$foo\n";    # prints 'foo'
}

#print "$foo\n";    # error under use strict, prints undef otherwise

{
  my $foo;          # a different lexical $foo
  print "$foo\n";    # prints undef
}

{
  our $bar = 'bar';  # sets $Foo::bar
  print "$bar\n";    # prints 'bar'
}

#print "$bar\n";    # error under use strict, prints 'bar' otherwise
print "$Foo::bar\n"; # prints 'bar'

{
  our $bar;          # same $Foo::bar
  print "$bar\n";    # prints 'bar'
}

Ronald
_____________________________________________________________
Seattle Perl Users Group Mailing List
    POST TO: spug-list at pm.org
SUBSCRIPTION: http://mail.pm.org/mailman/listinfo/spug-list
    MEETINGS: 3rd Tuesdays
    WEB PAGE: http://seattleperl.org/



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


More information about the spug-list mailing list