Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Syntactic Confectionery Delight
 
PerlMonks  

What is the purpose of '||=' in the example code for Spreadsheet::XLSX

by vt220 (Scribe)
on Nov 14, 2012 at 19:42 UTC ( #1003882=perlquestion: print w/ replies, xml ) Need Help??
vt220 has asked for the wisdom of the Perl Monks concerning the following question:

I tried a modified version of the example code given in http://search.cpan.org/~dmow/Spreadsheet-XLSX-0.13-withoutworldwriteables/lib/Spreadsheet/XLSX.pm. It works very well and now I am trying to understand it.

I am not sure what these two lines are doing.

$sheet -> {MaxRow} ||= $sheet -> {MinRow}; $sheet -> {MaxCol} ||= $sheet -> {MinCol};

My guess is that if MaxRow or MaxCol are undefined then they are set to equal MinRow or MinCol respectfully. If that is true then under what circumstances is MAX not defined?

Also, I Data::Dumper::Simple'd $excel and it appears MinCol and MaxCol does not vary by row i.e., it is set once for each worksheet. So why execute it on each row? Or did I read the dump incorrectly?

Following is the code I ran:

use strict; use warnings; use Data::Dumper::Simple; use Spreadsheet::XLSX; # use Text::Iconv; # my $converter = Text::Iconv -> new ("utf-8", "windows-1251"); # Text::Iconv is not really required. # This can be any object with the convert method. Or nothing. # my $excel = Spreadsheet::XLSX -> new ('test.xlsx', $converter); my $excel = Spreadsheet::XLSX -> new ($ARGV[0]); print Dumper($excel); foreach my $sheet (@{$excel -> {Worksheet}}) { printf("Sheet: %s\n", $sheet->{Name}); $sheet -> {MaxRow} ||= $sheet -> {MinRow}; foreach my $row ($sheet -> {MinRow} .. $sheet -> {MaxRow}) { $sheet -> {MaxCol} ||= $sheet -> {MinCol}; foreach my $col ($sheet -> {MinCol} .. $sheet -> {MaxCol}) { my $cell = $sheet -> {Cells} [$row] [$col]; if ($cell) { printf("( %s , %s ) => %s\n", $row, $col, $cell -> {Va +l}); } } } }

Thanks in advance!

Comment on What is the purpose of '||=' in the example code for Spreadsheet::XLSX
Select or Download Code
Re: What is the purpose of '||=' in the example code for Spreadsheet::XLSX
by tobyink (Abbot) on Nov 14, 2012 at 20:05 UTC

    "My guess is that if MaxRow or MaxCol are undefined then they are set to equal MinRow or MinCol respectfully."

    Very close. But it's not if they're undefined. It's if they're false. Undefined things are false, but so is 0, and so is the empty string, and so can be objects which overload boolification.

    The //= operator would do what you originally suggested. It specifically checks undefinedness, not falseness.

    The //= operator was introduced in Perl 5.10, so people wanting to do defined-or-assign while supporting ancient versions of Perl are forced to choose between ||= (which might not be quite what they want - maybe they need to differentiate between undef and 0) or a more longwinded:

    $sheet->{MaxRow} = $sheet->{MinRow} unless defined $sheet->{MaxRow};
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: What is the purpose of '||=' in the example code for Spreadsheet::XLSX
by jmlynesjr (Pilgrim) on Nov 14, 2012 at 20:45 UTC

    Programming Perl(4th) page 27

    $val ||="2"; # Set $val to 2 if it isn't already "true".

    James

    There's never enough time to do it right, but always enough time to do it over...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1003882]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (9)
As of 2014-04-23 20:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (554 votes), past polls