in reply to Need help with Excel::Writer::XLSX in using 'or'
Hello ravi45722,
This code snippet has a precedence problem:
foreach my $sub_user_error (@db_user_errors) { $worksheet->write( $row, $col, $db_error_data{$hour}{'Submissio +n_user_error'}{$sub_user_error} or 0, $number); $col++; }
If you look at the table in perlop#Operator-Precedence-and-Associativity, you’ll see that logical or has a lower precedence than the comma operator. So if the hash element contains a false value, the expression inside the call to write evaluates to 0, $number, which in turn evaluates to $number — which is not what you want.
The fix is to use the higher-precedence version of logical or, namely ||:
#! perl use strict; use warnings; use feature qw( say ); my $hour = 12; my $error = 'Submission_user_error'; my $sub_user_error = 3; my $number = 42; my %db_error_data; $db_error_data{$hour}{$error}{$sub_user_error} = 0; say 'With ||:'; say $db_error_data{$hour}{$error}{$sub_user_error} || 0, $number; say 'With or:'; say $db_error_data{$hour}{$error}{$sub_user_error} or 0, $number;
Output:
22:40 >perl 1619_SoPW.pl Useless use of private variable in void context at 1619_SoPW.pl line 1 +7. With ||: 042 With or: 0 22:40 >
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Need help with Excel::Writer::XLSX in using 'or'
by marioroy (Prior) on May 03, 2016 at 13:30 UTC |