[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] shift; vs my $temp = shift;



At 11:39 AM 11/22/00, Scott R. Godin wrote:
>on 11/22/2000 05:38 AM, Bart Lateur at bart.lateur@skynet.be wrote:
>
>>  On Wed, 22 Nov 2000 04:42:46 -0500, Scott R. Godin wrote:
>>
>>>  I had to change
>>>  sub size_calc {
>>>  shift;
>>>  ($_ <= 1024) ? return $_ . "k" : return sprintf('%.2f', ($_/1024)) .

[snip]
>>>
>>>  and I have no idea why. Any ideas?
>>
>>  You have no idea why? Doing.
>>
>>  shift() gets an item and you throw it away. shift() does NOT store it
>>  into $_, as you seem to assume. $_ is still what it was when you called
>>  this sub.
>>
>>  This would work, too:
>>
>>  sub size_calc {
>>  local $_ = shift;
>>  ... #same as rest of v1.
>>  }
>>
>>
>
>*incredulous head-scratching*
>
>Bart, the oddest thing is that it WAS working.. for at least two weeks(!)..
>and suddenly stopped as I've made further changes to the code.
>
>I don't remember what I changed, but obviously that's besides the point now.
>
>Thanks for the wake-up-call. Thought I was losing my mind. =:\

Just remember that $_ is not the same as @_. The latter holds the 
arguments being passed to a subroutine. When you use shift on it, the 
value you get is $_[0], the first item of the array @_.

It's possible your script was working because above the subroutine 
call you happened to have the correct value in $_, although that 
would be a risky way to handle values that you want to hang on to. 
Some programmers, in fact, use a standard of *never* putting values 
into $_ [not me, but then my code doesn't guide missiles...].

1;



- Bruce

__Bruce_Van_Allen___Santa_Cruz_CA__

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org