Link to home
Start Free TrialLog in
Avatar of meow00
meow00

asked on

shift ...

Hello experts,

  can anyone please tell me what the following code means ? may I have some examples as well ? thanks.

------------------------
sub Meow {
    my($cat) = shift;
    return int($cat + .1 * ($cat <=> 0));
}
-------------------------
Avatar of ozo
ozo
Flag of United States of America image

perldoc -f shift
       shift ARRAY
       shift   Shifts the first value of the array off and returns it, short-
               ening the array by 1 and moving everything down.  If there are
               no elements in the array, returns the undefined value.  If
               ARRAY is omitted, shifts the @_ array within the lexical scope
               of subroutines and formats, and the @ARGV array at file scopes
               or within the lexical scopes established by the "eval ''",
               "BEGIN {}", "INIT {}", "CHECK {}", and "END {}" constructs.

               See also "unshift", "push", and "pop".  "shift" and "unshift"
               do the same thing to the left end of an array that "pop" and
               "push" do to the right end.

so
  my($cat) = shift;
gets the first argument passed to the Meow sub
.1 * ($cat <=> 0)
is
-.1 if $cat < 0
  0 if $cat == 0
 .1 if $cat > 0
so
Meow(-2.91) is -3)
Meow(-2.89) is -2)
Avatar of meow00
meow00

ASKER

sorry ... i don't understand the following part :

-----------
Meow(-2.91) is -3)
Meow(-2.89) is -2)
--------------

why ???
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial