scope of $name in: (my $name = 123) && something-involving-$name;

Discussion in 'Perl Misc' started by Yakov, Nov 12, 2006.

  1. Yakov

    Yakov Guest

    I find it strange in the examlpe below

    1 open(my $fh, my $name="</no/such/file") ||
    2 warn "Error name=$name\n";
    3 print "name=$name";

    that $name is not in the scope, at line 2 (in the warn() arg).

    The assignment to $name in line 1 definitely already happened. So
    how come $name is not known at line 2 ? Is this a feature or a bug ?
    What is the explanation ? Does perl, like, wait for the semicolon to
    add $name to the hashtable or variables ?

    This is counterintuitive to me ... (comparing to C foo() { int x=1,
    y=x; } )

    Thanks
    Yakov
     
    Yakov, Nov 12, 2006
    #1
    1. Advertising

  2. Yakov wrote:
    > I find it strange in the examlpe below
    >
    > 1 open(my $fh, my $name="</no/such/file") ||
    > 2 warn "Error name=$name\n";
    > 3 print "name=$name";
    >
    > that $name is not in the scope, at line 2 (in the warn() arg).
    >
    > The assignment to $name in line 1 definitely already happened. So
    > how come $name is not known at line 2 ? Is this a feature or a bug ?
    > What is the explanation ? Does perl, like, wait for the semicolon to
    > add $name to the hashtable or variables ?


    perldoc perlsub
    [snip]
    The declared variable is not introduced (is not visible) until after
    the current statement. Thus,

    my $x = $x;

    can be used to initialize a new $x with the value of the old $x, and
    the expression

    my $x = 123 and $x == 123

    is false unless the old $x happened to have the value 123.


    So when perl compiles the line:

    open(my $fh, my $name="</no/such/file") || warn "Error name=$name\n";

    the second $name refers to a different variable then the first $name.

    $ perl -Mwarnings -Mstrict -ce'open(my $fh, my $name="</no/such/file") || warn
    "Error name=$name\n"'
    Global symbol "$name" requires explicit package name at -e line 1.
    -e had compilation errors.



    John
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
     
    John W. Krahn, Nov 12, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eddy Soeparmin

    Phone Format (770) 123-1234

    Eddy Soeparmin, Jul 28, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    3,215
    Swanand Mokashi
    Jul 28, 2003
  2. Jeremy Sanders

    Python 123 introduction

    Jeremy Sanders, Oct 30, 2006, in forum: Python
    Replies:
    7
    Views:
    293
    Bruno Desthuilliers
    Oct 31, 2006
  3. Ronald S. Cook

    Can I pass a parameter without using ?value=123

    Ronald S. Cook, May 1, 2007, in forum: ASP .Net
    Replies:
    4
    Views:
    455
    Page Brooks
    May 2, 2007
  4. Jesse Merriman

    [SOLUTION][QUIZ] Huffman Encoder (#123)

    Jesse Merriman, May 13, 2007, in forum: Ruby
    Replies:
    1
    Views:
    183
    Charles Lowe
    May 13, 2007
  5. Ruby Quiz

    [QUIZ] Huffman Encoder (#123)

    Ruby Quiz, May 11, 2007, in forum: Ruby
    Replies:
    11
    Views:
    346
    Drew Olson
    May 16, 2007
Loading...

Share This Page