Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

How scoping works in If statements?

by prasadbabu (Prior)
on May 30, 2008 at 06:23 UTC ( #689142=perlquestion: print w/replies, xml ) Need Help??

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:

# the traditional way if ($zippy) { print "Yow!"; } # the Perlish post-condition way print "Yow!" if $zippy;
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


Replies are listed 'Best First'.
Re: How scoping works in If statements?
by andreas1234567 (Vicar) on May 30, 2008 at 06:42 UTC
    I feel that writing
    my $x = 5 if (1);
    should cast a warning (when warnings are enabled), similarly as this would in perl 5.10:
    $ /usr/local/bin/perl5.10.0 -w my $x if (0); __END__ Deprecated use of my() in false conditional at - line 1.
    See perlsyn. It specifically says:
    NOTE: The behaviour of a my statement modified with a statement modifier conditional or loop construct (e.g. my $x if ...) is undefined. The value of the my variable may be undef, any previously assigned value, or possibly anything else. Don't rely on it. Future versions of perl might do something different from the version of perl you try it out on. Here be dragons.
    See also Re^2: Assign in loop with and without declaration for a related discussion.
    No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]
Re: How scoping works in If statements?
by linuxer (Curate) on May 30, 2008 at 09:21 UTC

    just my 2 cents and thoughts:

    If you had enabled warnings, you should have seen this warning:

    "my" variable $x masks earlier declaration in same scope at - line ...

    The two ways aren't equal because the first uses the "my $x = 5;" inside a block, so it has its own scope (the block); the second one is done without the block and is executed in the same scope as the "my $x = 10;" which causes that the warning is generated (if warnings are enabled).

    The scoping is done by the {} not by the if.

Re: How scoping works in If statements?
by mjemmeson (Monk) on May 30, 2008 at 08:46 UTC
    if you want to assign conditionally only using one line try the ternary operator:
    my $x = (1) ? 5 : undef;
      That still doesn't address the underlying scoping issue, though.
      use strict; use warnings; my $x = (0) ? 5 : undef; my $x = 42; __END__ "my" variable $x masks earlier declaration in same scope

      It's all fine and dandy until someone has to look at the code.
Re: How scoping works in If statements?
by impossiblerobot (Deacon) on May 30, 2008 at 11:47 UTC
    How about:
    do { my $x = 5 } if (1);
    The best/worst of both worlds? :-)

    Impossible Robot
Re: How scoping works in If statements?
by Anonymous Monk on May 30, 2008 at 09:53 UTC
    Hello Prasad, both the statments are wrong. when you write
    #case 2: my $x = 5 if (1); print $x; #prints 5
    for compiler $x is visible outside even though it does operations like case 1. so
    my $x=4 if(1); #x is as good as global for this file. if(1) { my $x=5; # x is visible inside the block for compiler so it makes it +inside. }
    so use braces if u want to make it part of if only.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://689142]
Approved by Corion
Front-paged by grinder
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2020-06-03 13:42 GMT
Find Nodes?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?

    Results (23 votes). Check out past polls.