Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > scope of $name in: (my $name = 123) && something-involving-$name;

Reply
Thread Tools

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

 
 
Yakov
Guest
Posts: n/a
 
      11-12-2006
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

 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      11-12-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Lexical scope vs. dynamic scope Xah Lee Java 0 02-26-2009 10:08 AM
CSPEC issue: lossing scope (or incorrect scope) in cspec subroutine. balldarrens@gmail.com Perl Misc 0 02-05-2009 08:42 PM
Scope - do I need two identical classes, each with different scope? ann Java 13 09-13-2005 03:07 AM
How do namespace scope and class scope differ? Steven T. Hatton C++ 9 07-19-2005 06:07 PM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM



Advertisments