unbalance (LHS)=RHS [wasRe: SPUG: 0 == undef]

Michael R. Wolf MichaelRWolf at att.net
Wed Apr 7 15:29:38 CDT 2004

"Peter Darley" <pdarley at kinesis-cem.com> writes:

> Folks,
> 	I'm wondering if there's any way to get 0 to not equal undef.  When I do:
> my ($Test1, $Test2) = 0, undef;

This is doing what you want, but not for the reasons you expect.
You've got array context on the LHS and scalar context on the RHS.

You probably wanted
    my ($Test1, $Test2) = (0, undef);

As written, you're not explicitly assigning anything to $Test2.

It's easier to see if I write it using numbers instead of 0 and undef;
    my ($Test1, $Test2) = 17, 99;
    print "$Test1\n";   # Outputs "17\n"
    print "$Test2\n";   # Outputs "\n"

Using parens, it looks like this
    (my ($Test1, $Test2) = (17)), 99;

What you wrote was like -- it's a list on both sides:
    ($Test1, $Test2) = (17);
Which is like:
    $Test2 = 17;        # the [0]'s go RHS to LHS
    $Test2 = undef;     # there's not a RHS[1]

As proven by perl -MO=Deparse 
- syntax OK
my($Test1, $Test2) = 17, '???';
print "$Test1\n";
print "$Test2\n";

The 99 is just the RHS of the (sparsely used) binary comma operator,
which evaluates the left side, then evaluates the right side, and
yeilds a the RHS.

Therefore, the expression evaluates to 99, which isn't used.

Michael R. Wolf
    All mammals learn by playing!
        MichaelRWolf at att.net

More information about the spug-list mailing list