Re: My, our, thems-over-theres.....

2001-09-03 Thread Bryan C . Warnock

A while ago, I posted some inconsistencies with scope declarators.
(http:[EMAIL PROTECTED]/msg08028.html)

A few more related notions


$a = "test";
{
chop(my $a = our $a = "hello");
  # The lexical is chopped
print "1-$a\n";
  # But the global is printed
}
print "2-$a\n";

# The assignments are evaluated right-to-left, resulting in the lexical 
# being chomped.  But the rightmost declaration is the one propogated 
# through the rest of the scope.

$b = "test";
{
chop($b = my $b = "hello");
  # The global is chopped
print "1-$b\n";
  # But the lexical is printed
}
print "2-$b\n";

# This one makes sense.  The lexical is created and initialized with 
# "hello".  That value is then passed to whatever the current $b in
# use is - the global (by default).  The lexical doesn't come fully into
# view until the next statement.

$c = "test";
{
chop(our $c = my $c = "hello");
  # Bizarre warning... but the global is chopped.
print "1-$c\n";
  # The lexical is printed
}
print "2-$c\n";

# Here, Perl recognizes that there were two variable declarations, 
# (since 'our' currently affects all subsequent variables on the same line)
# The global is still chopped, because it was the last one assigned, but
# the lexical takes affect, since it was the last one (in left-to-right
# order) declared.


Obviously, this is most heinous code to write.  And except for problems 
centered around when the declarations take affect, it warns appropriately.

'our' is a true accessor, meaning you can query the value, or you can set it.
'my' and 'local' aren't.  They set the value to 'undef' or the value you 
pass in.  (Perhaps it'd be easier to think of it as 'our $a = 
$__PACKAGE__::a')

In either case, that means an assignment is ultimately involved.  
Assignments are handled right-to-left, so I think the scope declarators 
should be too.  (Once all the other problems are fixed.)

-- 
Bryan C. Warnock
[EMAIL PROTECTED]



My, our, thems-over-theres.....

2001-08-14 Thread Bryan C . Warnock

Three variable scope declarators, three different behaviors (as recent as 
5.7.2)

(Set One)

$a = 'a';
{
my $a .= 'b', $a .= 'c' if $a .= 'd', $a .= 'e';
}
print $a, "\n"; # adec

$b = 'a';
{
local $b .= 'b', $b .= 'c' if $b .= 'd', $b .= 'e';
}
print $b, "\n";# ade

$c = 'a';
{
my $c = 'f';
our $c .= 'b', $c .= 'c' if $c .= 'd', $c .= 'e';
}
print $c, "\n";# adebc


(Set Two)

$a = 'a';
{
$a .= 'b', $a .= 'c' if my $a .= 'd', $a .= 'e';
}
print $a, "\n";# aebc

$b = 'a';
{
   $b .= 'b', $b .= 'c' if local $b .= 'd', $b .= 'e';
}
print $b, "\n";# a

$c = 'a';
{
my $c = 'f';
$c .= 'b', $c .= 'c' if our $c .= 'd', $c .= 'e';
}
print $c, "\n"; # ade

I'm sure this makes absolute sense under-the-hood, and it is documented 
(sort of) to behave this way, but isn't it a tad too inconsistent, even for 
Perl?  (Particularly 'my' vs 'our'.  'local' makes sense with its current 
behavior, but I'd personally rather it were consistent, too.)

-- 
Bryan C. Warnock
[EMAIL PROTECTED]