Excellent tutorial for me(newbie)
and yes Hugo, your example made it clear for me as I wrote out your example
use strict;
sub foo {
print "\$a is ", defined($a) ? $a : 'undef', $/;
print "\$b is ", defined($b) ? $b : 'undef', $/;
}
print "Just before start of scope\n";
{
print "Just after start of scope\n";
my $a = 1;
local $b = 1;
print "a=$a, b=$b\n";
foo();
print "Just before end of scope\n";
}
print "Just after end of scope\n";
print "\$a is ", defined($a) ? $a : 'undef', $/;
print "\$b is ", defined($b) ? $b : 'undef', $/;
Couple other questions/comments
1)using input record separator as newline indicator is very interesting. Never seen it before but works well. Any reason?
I am assuming it's just a style issue?
2)Below code from tutorial does not work when I insert my next to variable.. can someone explain this?
[root@myserver tmp]# cat -n !$
cat -n ./per_begin.pl1
1 #!/usr/bin/perl -w
2
3 use strict;
4
5
6 my $x = "original state";
7
8 sub foo {
9 print " \$x is: $x\n";
10 }
11
12 { # beginning of lexical scope
13
14 local $x = "altered state";
15 foo();
16
17 } # end of lexical scope
18 print "\$x is: $x\n";
[root@myserver tmp]# ./!$
././per_begin.pl1
Can't localize lexical variable $x at ././per_begin.pl1 line 14.
3)can someone explain further on "lexical variables are declared at compile-time, not initialised?
Is this because BEGIN runs during compile-time? I sneaked in a new my $foo = something inside of BEGIN
block and execution of the code came out w/ foo is something during BEGIN phase
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.