Perl : meaning of ||= symbol


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl : meaning of ||= symbol
# 1  
Old 09-14-2012
Perl : meaning of ||= symbol

While going through the below perl code, there is a line
which contains
Code:
$sheet -> {MaxRow} ||= $sheet -> {MinRow};

Could anyone please explain the meaning of ||= and where it is used

complete code
---------------
Code:
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 -> {Val});
                        }
 
                }
 
        }

---------- Post updated at 08:17 AM ---------- Previous update was at 07:16 AM ----------

Could anyone please explain the above code( ||= )
# 2  
Old 09-14-2012
That's logical OR assign operator.

Code:
   $sheet -> {MaxCol} ||= $sheet -> {MinCol};

probably meant that if $sheet -> {MaxCol} doesn't exists (returns false), assign the value of $sheet -> {MinCol} to this.

See perlop for more details.
# 3  
Old 09-14-2012
Short version:
x ||= y means that if x is false, set it to y.

Long version:
||= is an assignment operator. x ||= y is the same thing as x = x || y. || is the logical-OR operation. If the first operand (x) is a true value, it is returned. If it is false, the second operand (y) is returned. This is the typical logical-OR shortcircuiting behavior seen in C and many other languages.

Regards,
Alister

---------- Post updated at 09:58 AM ---------- Previous update was at 09:56 AM ----------

Quote:
Originally Posted by giridhar276
---------- Post updated at 08:17 AM ---------- Previous update was at 07:16 AM ----------
Don't bump your posts unless there is new, relevant information. Or at least wait a day. If it's an urgent matter, there's an Emergency forum you can use.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Meaning of $1^

Hello everyone, I'm looking for the meaning of this expression, as I don't understand it quite clearly : $1^ What do you think it could be? I thought either: - match lines starting with argument 1 but it should be ^$1 - turn line around : word becomes drow Thanks in advance for your... (4 Replies)
Discussion started by: bibelo
4 Replies

2. Shell Programming and Scripting

Meaning

Please let me know the meaning for the below statements in shell scripting. 1) exit -99 -------------------------------- 2) set prgdir = `pwd` set runFlag = runFlag:FALSE ------------------------------------- 3) if (-f $prgdir/maillst.eml) then set distEmail = `cat $prgdir/maillst.eml`... (1 Reply)
Discussion started by: lg123
1 Replies

3. Shell Programming and Scripting

Perl : Global symbol requires explicit package name Error while executing

I have executed the below perl script for copying the file from one server to another server using scp. #!/usr/bin/perl -w use Net::SCP::Expect; use strict; $server= "x.x.x.x"; my $source = "/mypath/mypath"; my $destination = "/home/"; print "Login...Starting scp..."; $user="admin";... (1 Reply)
Discussion started by: scriptscript
1 Replies

4. Shell Programming and Scripting

What is the meaning of the following Perl Code?

Hi All Can any one please help me to understand the below Perl Code package Win32::File; # # File.pm # Written by Douglas_Lankshear@ActiveWare.com # # subsequent hacks: # Gurusamy Sarathy # $VERSION = '0.06'; require Exporter; require DynaLoader; @ISA= qw( Exporter... (1 Reply)
Discussion started by: adisky123
1 Replies

5. Shell Programming and Scripting

Replace trailing whitespaces with pipe symbol using perl

I want to replace the whitespace with Pipe symbol to do a multiple pattern matching for the whole text "mysqld failed start" and same as for other text messages Below are the messages stored in a file seperate by whitespace mysqld failed start nfsd mount failed rpcbind failed to start for... (6 Replies)
Discussion started by: kar_333
6 Replies

6. UNIX for Dummies Questions & Answers

meaning of <<!

Hi all, I wanna know the meaning of the last word "<<! " sudo su - user <<! please help on this !!!! (1 Reply)
Discussion started by: sudharson
1 Replies

7. Shell Programming and Scripting

meaning of !*

can someone please tell what !* means in shell syntax. Regards, (3 Replies)
Discussion started by: busyboy
3 Replies

8. Solaris

/usr/lib/passwdutil.so.1: symbol __nsl_fgetspent_r: referenced symbol not found

deleteing post (0 Replies)
Discussion started by: dshakey
0 Replies

9. UNIX for Dummies Questions & Answers

what the meaning of #*

can some one please tell the meaning of the second statement i.e n=${m#*=} i couldnt get the meaning of the #*= 1.) m="mohit=/c/main/issue" echo $m result ----------- mohit=/c/main/issue 2.) n=${m#*=} echo $n RESULT ------- /c/main/issue (1 Reply)
Discussion started by: narang.mohit
1 Replies

10. AIX

meaning of ${0%${0##*/}}

. ${0%${0##*/}}Script_Name if i issue this command, it is executing the script. can any one tell what is the meaning of ${0%${0##*/}} (7 Replies)
Discussion started by: nyelavarthy
7 Replies
Login or Register to Ask a Question