understanding the ||= operator

Hello,=0A=0Afor the following program, what is the function of lines 12 and=
14 ???=0A=0A 1 #!/usr/bin/perl=0A 2 =0A 3 use strict;=0A 4 use warning=
s;=0A 5 use 5.010;=0A 6 use Spreadsheet::XLSX;=0A 7 =0A 8 my $excel =3D=
Spreadsheet::XLSX -> new ('Datos RCP 4_2_11-v2.xlsx');=0A 9 =0A 10 foreac=
h my $sheet ( [at] {$excel -> {Worksheet}}) {=0A 11 printf("Sheet: %s\n",=
$sheet->{Name});=0A 12 $sheet -> {MaxRow} ||=3D $sheet -> {MinRow};=
=0A 13 foreach my $row ($sheet -> {MinRow} .. $sheet -> {MaxRow}) {=
=0A 14 $sheet -> {MaxCol} ||=3D $sheet -> {MinCol};=0A 15 =
foreach my $col ($sheet -> {MinCol} .. $sheet -> {MaxCol}) {=
=0A 16 my $cell =3D $sheet -> {Cells} [$row] [$col];=
=0A 17 if ($cell) {=0A 18 =
printf("( %s , %s ) =3D> %s\n", $row, $col, $cell =0A-> {Val});=0A 19 =
}=0A 20 }=0A 21 }=0A 22 }=0A=0A=0A=


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Mailing Lists [ Fr, 11 Februar 2011 11:10 ] [ ID #2055019 ]

Re: understanding the ||= operator

>> 12 $sheet -> {MaxRow} ||=3D $sheet -> {MinRow};=0A>=0A>Line 12 ca=
n be written as:=0A>$sheet->{'MaxRow'} =3D $sheet->{'MaxRow'} || $sheet->{'=
MinRow'};=0A=0A=0Athen that I don't understand is the program logic :-(=0A=
=0Awhat's the purpose of lines 12 and 14?? =0A=0A=0A=0A

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Mailing Lists [ Fr, 11 Februar 2011 11:38 ] [ ID #2055020 ]

Re: understanding the ||= operator

--20cf3054a4d5dba41b049c05d54f
Content-Type: text/plain; charset=ISO-8859-1

It's the If $sheet->{MaxRow} is false[0], then the value of $sheet->{MinRow}
is assigned to that variable; If it's true, nothing happens.

Same deal with line 14; you canr ead more about the logical-or and other
operators in perldoc perlop[1].

Brian.

[0] http://perldoc.perl.org/perlsyn.html#Truth-and-Falsehood
[1] http://perldoc.perl.org/perlop.html

--20cf3054a4d5dba41b049c05d54f--
Brian Fraser [ Fr, 11 Februar 2011 19:26 ] [ ID #2055022 ]

Re: understanding the ||= operator

On Fri, Feb 11, 2011 at 5:38 AM, mailing lists <listas.correo [at] yahoo.es> wro=
te:
>>> 12 =C2=A0 =C2=A0 =C2=A0 =C2=A0$sheet -> {MaxRow} ||=3D $sheet -> {MinRo=
w};
>>
>>Line 12 can be written as:
>>$sheet->{'MaxRow'} =3D $sheet->{'MaxRow'} || $sheet->{'MinRow'};
>
>
> then that I don't understand is the program logic :-(
>
> what's the purpose of lines 12 and 14??

$sheet->{'MaxRow'} and $sheet->{'MinRow'} are used in the range in
line 13. Line 13 goes through a loop for every $row from MinRow to
MaxRow. If MaxRow isn't defined, the range operator in line 13 will
warn about uninitialized values. And if MaxRow isn't true
Spreadsheet::XLSX will probably complain since there is no row 0 in
Excel.

So line 12 checks to make sure that MaxRow is true. If MaxRow is true,
the ||=3D operator does nothing, but if MaxRow is false, the ||=3D
operator assigns the value of MinRow to it. This ensures that MaxRow
is always true (unless MinRow is also undef, which would be a bigger
problem) and that line 13 functions as expected. If MinRow =3D=3D MaxRow
(i.e. there is only one row), the loop only executes once.

HTH,

-- j
--------------------------------------------------
This email and attachment(s): [=C2=A0 ] blogable; [ x ] ask first; [=C2=A0 =
]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com=C2=A0 http://www.downloadsquad.com=C2=A0 http://www.eng=
atiki.org

values of =CE=B2 will give rise to dom!

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
daggerquill unknown [ Fr, 11 Februar 2011 21:03 ] [ ID #2055023 ]

Re: understanding the ||= operator

On 11/02/2011 10:38, mailing lists wrote:
>>> 12 $sheet -> {MaxRow} ||= $sheet -> {MinRow};
>>
>> Line 12 can be written as:
>> $sheet->{'MaxRow'} = $sheet->{'MaxRow'} || $sheet->{'MinRow'};
>
>
> then that I don't understand is the program logic :-(
>
> what's the purpose of lines 12 and 14??

Perl has no proper boolean values. Instead, the boolean operators treat
zero, undef, and the null string '' all as false. Anything else is true.

So

'' || 'xx'

evaluates to true, because although the left operand is false, the right
one is true and so the 'or' of the two is true.

The reason code like

$x = $x || $y

or the equivalent

$x ||= $y

are useful is because of the way Perl evaluates the expression. It is
called short-circuit evaluation, and is described here

<http://www.perlmonks.org/?node_id=301355>
and
<http://en.wikipedia.org/wiki/Short-circuit_evaluation>

Essentially,

$x ||= $y

is equivalent to

if ($x) {
$x = $x;
}
else {
$x = $y;
}

and, clearly, assigning $x to itself has no effect, so we have

if (not $x) {
$x = $y;
}

or

$x = $y if not $x;

So line 12 of your code can be replaced with

$sheet->{MaxRow} = $sheet->{MinRow} if not $sheet->{MaxRow};

which you may prefer, and I would find it hard not to agree with you.

HTH,

Rob

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Rob Dixon [ Fr, 11 Februar 2011 23:06 ] [ ID #2055024 ]

Re: understanding the ||= operator

>>>>> "RD" == Rob Dixon <rob.dixon [at] gmx.com> writes:

RD> On 11/02/2011 10:38, mailing lists wrote:
>>>> 12 $sheet -> {MaxRow} ||= $sheet -> {MinRow};
>>>
>>> Line 12 can be written as:
>>> $sheet->{'MaxRow'} = $sheet->{'MaxRow'} || $sheet->{'MinRow'};
>>
>>
>> then that I don't understand is the program logic :-(
>>
>> what's the purpose of lines 12 and 14??

RD> Perl has no proper boolean values. Instead, the boolean operators
RD> treat zero, undef, and the null string '' all as false. Anything else
RD> is true.

to be pedantic, '0' is also false. it isn't exactly the same as 0.

uri

--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Uri Guttman [ Sa, 12 Februar 2011 04:47 ] [ ID #2055045 ]

one question about an exercise in 'Learning Perl 4th Edition'

SGksDQoNCkkgaGF2ZSBvbmUgcXVlc3Rpb24gYWJvdXQgdGhlIGV4ZXJjaXNl IE5vLiA0ICBpbiBj
aGFwdGVyIDcgLg0KDQpIZXJlIGlzIHRoZSBleGVyY2lzZSA6DQpNYWtlIGEg cHJvZ3JhbSB0aGF0
IHByaW50cyBlYWNoIGxpbmUgd2l0aCBhIHdvcmQgdGhhdCBpcyBjYXBpdGFs aXplZCBidXQgbm90
IEFMTCBjYXBpdGFsaXplZC4gRG9lcyBpdCBtYXRjaCBGcmVkIGJ1dCBuZWl0 aGVyIGZyZWQgbm9y
IEZSRUQ/DQpBcyBzdWdnZXN0ZWQgaW4gdGhlIGV4ZXJjaXNlLCBJIGNyZWF0 ZWQgYSBzbWFsbCB0
ZXh0IGZpbGUgYWRoZXJlZCBiZWxvdyB0ZXN0aW5nIHJlY29yZHM6DQoNCiAg ICAgICAgICAgICAg
ICBmcmVkDQphYWFhYQ0KYWZyZWQNCkZSRUQNCmZyZWVkDQpGcmVkDQpNci5T bGENCkFsZXhmcmVk
DQpudW1iZXIxMjNGcmVkDQoNCkhlcmUgaXMgbXkgY29kZToNCg0KIy0tLS0t LS0tLS0tLS0tLS1j
b2RlIHN0YXJ0LS0tLS0tLS0tLS0tLS0tLS0tLS0tDQoNCiMhcGVybA0KDQp1 c2Ugd2FybmluZ3M7
DQp1c2Ugc3RyaWN0Ow0KDQpkaWUgIkZhaWxlZCB0byBvcGVuIGZpbGUgOiQh XG4iIGlmICggIW9w
ZW4gTVlGSUxFLCAiYzpcXHRlc3RkYXRhLnR4dCIgKTsNCg0KZm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MgY29udGVu dHMgdG8gYW55IG90
aGVyIHBlcnNvbiBhbmQgDQphbnkgc3VjaCBhY3Rpb25zIGFyZSB1bmxhd2Z1 bC4gVGhpcyBlLW1h
aWwgbWF5IGNvbnRhaW4gdmlydXNlcy4gSW5mb3N5cyBoYXMgdGFrZW4gDQpl dmVyeSByZWFzb25h
YmxlIHByZWNhdXRpb24gdG8gbWluaW1pemUgdGhpcyByaXNrLCBidXQgaXMg bm90IGxpYWJsZSBm
b3IgYW55IGRhbWFnZSANCnlvdSBtYXkgc3VzdGFpbiBhcyBhIHJlc3VsdCBv ZiBhbnkgdmlydXMg
aW4gdGhpcyBlLW1haWwuIFlvdSBzaG91bGQgY2Fycnkgb3V0IHlvdXIgDQpv d24gdmlydXMgY2hl
Y2tzIGJlZm9yZSBvcGVuaW5nIHRoZSBlLW1haWwgb3IgYXR0YWNobWVudC4g SW5mb3N5cyByZXNl
cnZlcyB0aGUgDQpyaWdodCB0byBtb25pdG9yIGFuZCByZXZpZXcgdGhlIGNv bnRlbnQgb2YgYWxs
IG1lc3NhZ2VzIHNlbnQgdG8gb3IgZnJvbSB0aGlzIGUtbWFpbCANCmFkZHJl c3MuIE1lc3NhZ2Vz
IHNlbnQgdG8gb3IgZnJvbSB0aGlzIGUtbWFpbCBhZGRyZXNzIG1heSBiZSBz dG9yZWQgb24gdGhl
IA0KSW5mb3N5cyBlLW1haWwgc3lzdGVtLg0KKioqSU5GT1NZUyoqKioqKioq IEVuZCBvZiBEaXNj
bGFpbWVyICoqKioqKioqSU5GT1NZUyoqKg==
Alex Wang02 [ Sa, 12 Februar 2011 05:42 ] [ ID #2055047 ]

RE: one question about an exercise in 'Learning Perl 4th Edition'

SGksDQoNCkkganVzdCBmb3VuZCB0aGUgc29sdXRpb24gYW5kIHJlYXNvbiBp biBDaGFwdGVyIDgu
DQoNClRoYW5rcyAmIFJlZ2FyZHMsDQoNCkFsZXggV2FuZw0KDQoNCi0tLS0t T3JpZ2luYWwgTWVz
c2FnZS0t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YWlsIG9yIGl0cyBjb250 ZW50cyB0byBhbnkg
b3RoZXIgcGVyc29uIGFuZCANCmFueSBzdWNoIGFjdGlvbnMgYXJlIHVubGF3 ZnVsLiBUaGlzIGUt
bWFpbCBtYXkgY29udGFpbiB2aXJ1c2VzLiBJbmZvc3lzIGhhcyB0YWtlbiAN CmV2ZXJ5IHJlYXNv
bmFibGUgcHJlY2F1dGlvbiB0byBtaW5pbWl6ZSB0aGlzIHJpc2ssIGJ1dCBp cyBub3QgbGlhYmxl
IGZvciBhbnkgZGFtYWdlIA0KeW91IG1heSBzdXN0YWluIGFzIGEgcmVzdWx0 IG9mIGFueSB2aXJ1
cyBpbiB0aGlzIGUtbWFpbC4gWW91IHNob3VsZCBjYXJyeSBvdXQgeW91ciAN Cm93biB2aXJ1cyBj
aGVja3MgYmVmb3JlIG9wZW5pbmcgdGhlIGUtbWFpbCBvciBhdHRhY2htZW50 LiBJbmZvc3lzIHJl
c2VydmVzIHRoZSANCnJpZ2h0IHRvIG1vbml0b3IgYW5kIHJldmlldyB0aGUg Y29udGVudCBvZiBh
bGwgbWVzc2FnZXMgc2VudCB0byBvciBmcm9tIHRoaXMgZS1tYWlsIA0KYWRk cmVzcy4gTWVzc2Fn
ZXMgc2VudCB0byBvciBmcm9tIHRoaXMgZS1tYWlsIGFkZHJlc3MgbWF5IGJl IHN0b3JlZCBvbiB0
aGUgDQpJbmZvc3lzIGUtbWFpbCBzeXN0ZW0uDQoqKipJTkZPU1lTKioqKioq KiogRW5kIG9mIERp
c2NsYWltZXIgKioqKioqKipJTkZPU1lTKioq
Alex Wang02 [ Sa, 12 Februar 2011 06:24 ] [ ID #2055048 ]

Re: understanding the ||= operator

Hi,

> 12 $sheet -> {MaxRow} ||= $sheet -> {MinRow};

Line 12 can be written as:
$sheet->{'MaxRow'} = $sheet->{'MaxRow'} || $sheet->{'MinRow'};

For example:
$variable_1 ||= $variable_2 is equivalent to $variable_1 = $variable_1
|| $variable_2.

The same applies to:

**= += *= &= <<= &&=
-= /= |= >>= ||=
.= %= ^= //=
x=

Regards,
Alan Haggai Alavi.
--
The difference makes the difference

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Alan Haggai Alavi [ Fr, 11 Februar 2011 11:26 ] [ ID #2055050 ]

Re: understanding the ||= operator

Hi,

> then that I don't understand is the program logic :-(

It is a logical OR. Quoting `perldoc perlop`:

C-style Logical Or
Binary "||" performs a short-circuit logical OR operation. That
is, if the left operand is true, the right operand is not even
evaluated. Scalar or list context propagates down to the right
operand if it is evaluated.

$sheet->{'MaxRow'} = $sheet->{'MaxRow'} || $sheet->{'MinRow'};

Equivalent to:

unless ( $sheet->{'MaxRow'} ) {
$sheet->{'MaxRow'} = $sheet->{'MinRow'};
}

In English:
If $sheet->{'MaxRow'} has a value that evaluates to false, make
$sheet->{'MaxRow'} equal to $sheet->{'MinRow'}.

Though I have left the else clause,

By the way, it is not recommended to use `unless` for anything complex
as it can get in the way of ordinary thinking process. Use `if` and
`!`.

Regards,
Alan Haggai Alavi.
--
The difference makes the difference

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Alan Haggai Alavi [ Fr, 11 Februar 2011 11:56 ] [ ID #2055051 ]

Re: understanding the ||= operator

On 2011-02-11 11:26, Alan Haggai Alavi wrote:

> $variable_1 ||= $variable_2 is equivalent to
> $variable_1 = $variable_1 || $variable_2.

Hmm, I don't buy that, I would say that $x ||= $y is equivalent to

$x = $y unless $x;

alternatively:

$x or $x = $y;

because the setting of $x only needs to happen if $x is false.

Realize that C<$variable_1 = $variable_1;> can only be optimized away if
no magic is involved.

--
Ruud

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
rvtol+usenet [ Sa, 12 Februar 2011 12:13 ] [ ID #2055053 ]

Re: understanding the ||= operator

On Sat, 12 Feb 2011 12:13:13 +0100, Dr.Ruud wrote:
> On 2011-02-11 11:26, Alan Haggai Alavi wrote:
>
>> $variable_1 ||= $variable_2 is equivalent to $variable_1 = $variable_1
>> || $variable_2.
>
> Hmm, I don't buy that, I would say that $x ||= $y is equivalent to
>
> $x = $y unless $x;
>
> alternatively:
>
> $x or $x = $y;
>
> because the setting of $x only needs to happen if $x is false.
>
> Realize that C<$variable_1 = $variable_1;> can only be optimized away if
> no magic is involved.

No, the optimization seems to happen either way:

$ perl -E 'tie $x, "F"; tie $y, "F"; say "Orcish:"; $x ||= $y; say
"Unwrapped:"; $x = $x || $y; say "Done"; sub F::TIESCALAR{ bless [],
"F" }; sub F::AUTOLOAD{ say $F::AUTOLOAD } '
Orcish:
F::FETCH
Unwrapped:
F::FETCH
Done
F::DESTROY
F::DESTROY


--
Peter Scott
http://www.perlmedic.com/ http://www.perldebugged.com/
http://www.informit.com/store/product.aspx?isbn=0137001274
http://www.oreillyschool.com/courses/perl3/

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Peter Scott [ Mo, 14 Februar 2011 06:16 ] [ ID #2055135 ]
Perl » gmane.comp.lang.perl.beginners » understanding the ||= operator

Vorheriges Thema: DBD::mysql::st execute failed
Nächstes Thema: perl's threads