[Wellington-pm] 'my' and 'our'

Jacinta Richardson jarich at perltraining.com.au
Wed May 3 03:57:05 PDT 2006


my is for lexical variables.  That is variables that exist and can only be seen
in their current scope.  A variable's scope is defined as the smallest enclosing
block (including file).

our is for package variables (think globals).  These are variables that exist
and can be accessed outside of the scope in which they were declared, for
example in different files.

Consider this example:

----------------------
package Foo;

my $bar;
our $baz;

{    # open new scope

     my $gatx;
     print $gatx;    # works
}

print $gatx;         # doesn't work (error)

----------------------
# main program
use Foo;

print $Foo::baz;    # works
print $Foo::bar;    # error, $bar can't be seen out here.

-----------------------

our replaces the need for "use vars" from code prior to 5.6.  It's a tiny bit
more complicated than this, but not so much that it matters.


If this doesn't help, please feel free to ask more questions.

All the best,

	Jacinta

-- 
   ("`-''-/").___..--''"`-._          |  Jacinta Richardson         |
    `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
    (_Y_.)'  ._   )  `._ `. ``-..-'   |      +61 3 9354 6001        |
  _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
 (il),-''  (li),'  ((!.-'             |   www.perltraining.com.au   |




More information about the Wellington-pm mailing list