There are basically two kinds of variables in Perl. However, there are three ways to declare a variable (in the latest version of Perl). Of course, this will be much easier to understand with some examples.

 

Here's an example with use vars. Note that, although the use vars is within a block, it takes effect for the whole file. However, after the switch to package main, $var refers to $main::var.

#!perl -l package Pkg; { use vars qw/ $var /; $var = 7; print $var; # 7 } print $var; # 7 package main; print $var; # undef; $main::var has not been set # error with strict print $Pkg::var; # 7

 

Here's an example with my. Note that only within the lexical block can you access the $var that holds the number 7.

#!perl -l package Pkg; { my $var = 7; print $var; # 7 } print $var; # undef; $Pkg::var has not been set # error with strict package main; print $var; # undef; $main::var has not been set # error with strict print $Pkg::var; # undef; $Pkg::var has not been set

 

Here's an example with our. Note the behavior outside the block before the switch to main; $var refers to the $Pkg::var package variable, but without the package qualifier it would be an error with use strict.
#!perl -l package Pkg; { our $var = 7; print $var; # 7 } print $var; # 7 # error with strict package main; print $var; # undef; $main::var has not been set # error with strict print $Pkg::var; # 7

 

Here's another example with our. Note that, after the switch to package main, $var still refers to $Pkg::var, because we're still in the same lexical scope.
#!perl -l package Pkg; our $var = 7; print $var; # 7 package main; print $var; # 7; even though we switched packages, # $var still refers to $Pkg::var # in this lexical scope print $Pkg::var; # 7
So, both use vars and our declare a variable in the current package. The difference between them is in how you can access the variable. With either one, you can always use $Pkg::var from anywhere. With use vars, you can use $var anywhere in the file, as long as you're in the right package. With our and use strict, you can only use $var within the lexical scope, but you can still use $var after switching packages.

Finally, to answer your question about which of three declarations to use at the top of a script. If you're only accessing the variables from that file, you can use my. If you don't switch to a different package later on in the file, use vars and our are equivalent. If you do switch packages, our could result in surprising behavior, as in the last example above.

 

Update: echo pointed out a few places where I had written $variable instead of $var.

In reply to Re: Our, use vars, and magic, oh my! by chipmunk
in thread Our, use vars, and magic, oh my! by OzzyOsbourne

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.