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
In reply to How scoping works in If statements? by prasadbabu
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |