Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Syntactic Confectionery Delight
 
PerlMonks  

Re: Perl array declaring

by shmem (Canon)
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.


Comment on Re: Perl array declaring
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1036512]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (16)
As of 2014-04-17 15:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (452 votes), past polls