>>>>> "JSJ" == J S John <phillyj...@gmail.com> writes:

  JSJ> Hi all, I'm new to Perl. The only other language I know is
  JSJ> Matlab/Octave and I'm still working my way around Linux. I am using
  JSJ> Shlomi Fish's tutorial on perl until I get the llama book. I'm stuck
  JSJ> on section [4.1. "+=" and friends ] [1]. I don't really understand the
  JSJ> part "$a *= 2; $b += 1;" What is the *= mean?

others have given you the direct answer. here is the more general one
which you need to learn.

anytime you see an assignment operator which is always a two char op
ending in =, there is a simple rule to expand it.

        $x += $y        is $x = $x + $y
        $x -= $y        is $x = $x - $y
        $x *= $y        is $x = $x * $y

those (and other) assignment operators say to take the lvalue (the
location being assigned to) and make it the left side of the binary
operator in front of the =. take that result and assign it to the
lvalue.

i say lvalue (meaning something which can be on the left side of =,
i.e. it can be assigned to) because it can be expressions other than
scalar vars:

        $totals{foo} += $foo_count ;

so += and friends are not only a shortcut when coding, they can be more
efficient since they only evaluate the lvalue one time and that can save
if that is hash/array or deeper access. and that keeps the logic cleaner
as well.

the most commonly used assignment ops are ||=, +=, .=, *= and maybe
-=. 

||= is effectively a way to assign a default value. it doesn't work if
the lvalue already has either 0 or '' in it. the newer //= handles that
case as it only tests for defined. these are the same:

        $arg //= 'default' ; 
        $arg = $arg // 'default' ;


and finally, when you don't know what a perl operator is, read perldoc
perlop. the section on assignment operators covers this and shows all
the legal types (16 of them).

uri

-- 
Uri Guttman  ------  u...@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-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to