Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
(those declared with our or with a fully qualified name)

According to the docs, variable symbols declared with our are lexical symbols (although the symbol table entry they refer to is a package global):

our associates a simple name with a package variable in the current package for use within the current scope. When use strict 'vars' is in effect, our lets you use declared global variables without qualifying them with package names, within the lexical scope of the our declaration. In this way our differs from use vars , which is package scoped.

What makes them look like non-lexicals is their auto-vivification of package globals.

Consider:

# file foo our $foo = 'hello world!';
#!/usr/bin/perl use strict; $\ = "\n"; print "no \$foo" unless defined $main::{foo}; require 'foo'; # or do 'foo' my $first = <<'EOH'; print "\$foo defined" if defined $main::{foo}; EOH eval $first; my $second = <<'EOH'; print $main::foo; EOH eval $second; __END__ no $foo $foo defined hello world!

and

#!/usr/bin/perl use strict; $\ = "\n"; print "no \$foo" unless defined $main::{foo}; require 'foo'; # or do 'foo' print $main::foo;

The string evals show that our effectively allocates a symbol table entry for the package. The second example shows that after the compilation phase a symbol table entry main::foo has been allocated.

I think of our as a hybrid beast. It is global in the sense that it generates a global, and a variable declared with our is an alias to the symbol table entry. It is lecical however in that it is transparent to multiple packages in a file.

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

In reply to Re^2: overview/tutorial on packages - creation and use by shmem
in thread overview/tutorial on packages - creation and use by chexmix

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found