I have taken folloing from a post on this forum. Still I could not make out few things.
use vars qw/$x, @y, %z, $d, @e, %f, $p, @q, %r/;
$a = 10;
my ($b) ; # what is the meaning of bracket around variable name ?
{
# ...
my ($p, @q, %r);
local ($d, @e, %f); # (1)
# ...
sub foo { # ...
}
# ...
foo(17); # (2)
bar($p,29); # (3)
lbar($d, 31);
# ...
} # (4)
# ...
sub bar { # ...
}
{
# ...
local ($x, @y, %z); # (5)
local $a = 20;
my $a = 30;
# ...
sub baz { # ...
}
# ...
baz(17); # (6)
quux($x,29); # (7)
# ...
sub child {
$b = a * 10; # which value of \$a will be available here ?
}
} # (8)
# ...
sub quux { # ...
}
# ...
baz(29); # (9)
1) Can global variable be declared anywhere in the code or only at the beginning of the code ?
2) If local variable and my variable both are declared using same name, is that allowed or is that an error ? If global variable and local variable , both have same name and I want to use global variable in the scope where my $a variable is declared, I can call it using $::a. In the child(), I have not declared $a and I want to call global value of $a in the child(). If I again use $::a, will it call the global value of $a or will it be value my $a ? If call $a in the child(), without using $::a, will it still call global value of $a or my $a declared in the scope in which child() is declared ?
3) If thats allowed, and in the sub(), if I am using variable name, which valkue will be taken in sub()
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 () ?
5) Declaring variables at the beginning of the code using "use vars", is it the only way to declare global variables ? or just declaring variables in the beginning of the code without local or my makes them global variables ?
6) what is the difference between my $b; and my ($b);
7) If my variable is declared to make its scope local, then whats the point in declaring my variable at the top level ? Like the "my ($b)" or my $c in this example ?
8) Is it true that my $p will not be available to bar() , as bar() is defined outside the scope of my ($p, @q, %r) and only global $p will be available ? But local $d will be available to lbar(), as lbar() is called within the in time scope of $d and it will not use global value of $d?
Please excuse me , if some questrions sound like repeated.
Thanks
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.