prasadbabu has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks,
Good Day.
Today in one of my program, I got wrong output and when I went through the code, it is because of scoping problem in if statements.
Then I went through the Perl Documentation and it says the following:
Note that the braces are required in Perl, even if you've only got one line in the block. However, there is a clever way of making your one-line conditional blocks more English like:
When I tested with example, I am getting the following outputs
my $x = 10; #case 1: if (1){ my $x = 5; } print $x; #prints 10 #case 2: my $x = 5 if (1); print $x; #prints 5
I know the variable declared with my inside a block will have scoping till the block ends, which works fine in case 1, but not in case 2. If so, then how the document says both are equal and the difference is Perlish way.
Is both statements are equal or different? Could someone enlighten me?
Thanks in advance
Prasad
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: How scoping works in If statements?
by andreas1234567 (Vicar) on May 30, 2008 at 06:42 UTC | |
Re: How scoping works in If statements?
by linuxer (Curate) on May 30, 2008 at 09:21 UTC | |
Re: How scoping works in If statements?
by mjemmeson (Monk) on May 30, 2008 at 08:46 UTC | |
by kwaping (Priest) on May 30, 2008 at 18:17 UTC | |
Re: How scoping works in If statements?
by impossiblerobot (Deacon) on May 30, 2008 at 11:47 UTC | |
Re: How scoping works in If statements?
by Anonymous Monk on May 30, 2008 at 09:53 UTC |