Steve Bertrand wrote:
Hi all,
Before chasing down and fixing an undef var earlier on in a trace path,
I had the following code. I've finally gathered that fixing bugs and
validating data upstream is far better than writing code to fix it later
(of course, I had to write code to find out what was happening).
The last two lines do not do what I originally was hoping they would.
The code has been fixed.
I _thought_ that if I use the 'or assign' op, it would either append the
$payment amount to the existing hash element, or if it didn't exist, it
would create one from the current $payment.
I recalled that a hash element will be created dynamically, which
rendered my code useless, and is what lead me to perform further
diagnostics and trace upstream.
What I want to know, is if someone could place parens to help me better
understand the precedence order in the last two lines. I know they are
legal as they do work, but I don't know how the interpreter is
interpreting them:
while ( my $ledger_ref = $sth->fetchrow_hashref ) {
# print Dumper $ledger_ref;
# next();
my $amount = $ledger_ref->{amount};
my $payment = $ledger_ref->{payment};
my $username = $ledger_ref->{username};
# print "$amount :: $payment :: $username\n";
$user_ref->{$username}{payment} += $payment ||= $payment;
$user_ref->{$username}{amount} += $amount ||= $amount;
Assignments happen right-to-left so, for instance, this will work:
my $x = my $y = my $z = some_value();
where $z will be assigned to first and then $y will be assigned the
result of the first assigment and $x will be assigned the result of the
second assignment.
$ perl -MO=Deparse,-p -e'my $x = my $y = my $z = some_value();'
(my $x = (my $y = (my $z = some_value())));
-e syntax OK
John
--
Those people who think they know everything are a great
annoyance to those of us who do. -- Isaac Asimov
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/