Short version:

local doesn't declare variables. It saves the value of a package variable and makes it so the value gets restored when the lexical scope is exited.


1) Can global variable be declared anywhere in the code or only at the beginning of the code ?

Lexical variables can be declared anywhere an expression can be found.

Package variables need not be declared. use vars can be present anywhere a statement can be found.

our, which creates a lexical alias to a package var, can be present anywhere an expression can be found.

2) If local variable and my variable both are declared using same name, is that allowed or is that an error ?

local doesn't declare variables. It saves the value of a package variable and makes it so the value gets restored when the lexical scope is exited.

In our vs my, the latter wins IIRC. Easy to test.

4) If a sub() is declared within a sub(), can the variables declared in super sub() be accessed in child sub() without being declared in child () ?

If the inner sub is an anonymous sub, yes. Even if the outer sub has exited before the inner sub has exited.

>perl -E"sub mk { my $x=$_[0]; sub { $x } } $x=mk('a'); $y=mk('b'); s +ay $x->(),$y->()" ab

Don't nest named subs. You'll likely end up with buggy code (a warning will be issued if you have them on and the buggy situation arises), and it doesn't serve any purpose (the inner sub isn't public).

5) Declaring variables at the beginning of the code using "use vars", is it the only way to declare global variables ?

use vars and our.

or just declaring variables in the beginning of the code without local or my makes them global variables ?

Global variables are created on the fly whenever they are used.

6) what is the difference between my $b; and my ($b);

The latter is considered a list in order to determine if an assignment operator is a scalar assignment operator or a list assignment operator.

# The "=" is a scalar assignment operator, so: # - f() is evaluated in scalar context. # - The assignment returns $x. my $x = f(); # The "=" is a list assignment operator, so: # - f() is evaluated in list context. # - The assignment returns ($x) in list context, or # - the number of items returned by f() in scalar context. my ($x) = f();

7) If my variable is declared to make its scope local

local doesn't declare variables. local doesn't change the scope of a variable.


In reply to Re: Understanding difference between my and local variables. by ikegami
in thread Understanding difference between my and local variables. by manishrathi

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.