Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: Perl array declaring

by shmem (Chancellor)
on Jun 01, 2013 at 22:34 UTC ( #1036512=note: print w/replies, xml ) Need Help??

in reply to Perl array declaring

See perldata.

A variable (a symbol which lets you reference some data) in perl is ruled by two constraints, or boundaries, or scopes:

  • the package (or namespace) scope
  • the lexical scope

Package scoped variables are recorded in the symbol table of their associated namespace.
The default namespace is main, and its symbol table is accessible via the hash %main:: (or shorthand %::).
This scope can span files.

Lexical scoped variables are visible only in the current BLOCK, file or eval.
These variables are not associated with any symbol table, but rather with their (static) execution path.

The package variables are either just written without any declaration (e.g $file = "myfile.txt"; - the strict pragma forbids that), used fully qualified (e.g. $main::file = "myfile.txt"; (that is ok under strict) or declared with our (e.g. our $file = "myfile.txt";). The our creates a symbol table entry for a variable declared that way and a lexical alias for that symbol.

The lexical variables are declared with my (e.g. my $file = "myfile.txt";). At runtime, they are reset when their scope is entered.

Just for completeness - a package variable can be used in a lexical sort of way, too - masking the original symbol table variable slot with a local declaration. They are valid during the execution time in the current scope, even branching to different files.

Phew. Now back to your question:

Hello i am a new to Perl and i am trying to understand what is differences between declare :my @array or @arry?

Short answer:

  • my @array is a named array declaration, lexically scoped, visible only in the enclosing block (delimited with curlies), eval or file
  • @arry is a (unqualified) package variable which on first ocurrance causes the creation of an entry in the current symbol table

If anything of the above is unclear, just ask again.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1036512]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2018-03-17 06:47 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (223 votes). Check out past polls.