iliya24 has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: Perl array declaring
by shmem (Chancellor) on Jun 01, 2013 at 22:34 UTC

    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.

Re: Perl array declaring
by thomas895 (Deacon) on Jun 01, 2013 at 20:21 UTC

    Generally you will want to declare your variables when you first use them. In most cases my is enough, but for a global variable you will want to use our. See perlintro#Perl variable types and Variable scoping for more information.
    Also, when your code increases in size, it is useful to know where your data is coming from, and to see if you've already used it or not. Say you have an array, @stuff, that you fill early on, and need much later. But somewhere in between, you overwrite that array with different contents, forgetting its earlier use.
    It can be illustrated like so:

    #note: "use strict" is NOT done here #at first: @stuff = ( 1, 2, 4, 8, 16 ); #several hundered lines of code @stuff = ( "foo", "bar", "baz" ); do_something_with_words(@stuff); #and much later: do_something_with_numbers(@stuff);

    The method do_something_with_numbers will probably fail. So use strict and warnings, as they will save you much headache when it doesn't work.

    ~Thomas~ 
    "Excuse me for butting in, but I'm interrupt-driven..."
Re: Perl array declaring
by locked_user sundialsvc4 (Abbot) on Jun 02, 2013 at 16:54 UTC

    Especially to one “new to Perl,” let us repeat this very critical mantra:   always begin your programs with:

    use strict;
    use warnings;

    This will probably cause new error-messages to be generated, and you should adjust your source-code as needed to eliminate them.   Quite a number of bugs and “inexplicable behaviors” will be squeezed-out by doing this.

    Perl is very much a “DWIM = Do What I Mean” language.   Rightly or wrongly, it doesn’t impose many strictures upon what you write.   It will try to adopt an interpretation that works, and, with it, to do what it thinks you mean.   These two declarations will cause it be less-tolerant and more-strict ... flushing-out more errors at compile time.   (The antonym of use is no, which you will occasionally also see in production code.)

Re: Perl array declaring
by Anonymous Monk on Jun 01, 2013 at 17:58 UTC
    When strict refs, we should declare a variable like my @string to use it in the program. A variable can be declared only once in a single program.

      Variables can be declared as many times as you like...

      use strict; our $foo = 123; our $foo; our $foo; our $foo; our $foo; print(our $foo, "\n");

      Also, strict refs don't force you to declare a variable...

      use strict "refs"; $foo = 123; # ok print "$foo\n"; # still ok

      ... it is strict vars that forces you to declare variables.

      package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
        Variables can be declared as many times as you like...
        True only for global variables, and even then, not without warnings. Change your 'our' to 'my' and your program no longer works. You are creating new variables with the same name. Warnings inform you that the new variable masks an earlier declaration.
        Bill