what "shift" does, if not "$_ = shift;" ?

what "shift" does, if not "$_ = shift;" ?

am 29.11.2007 19:25:24 von devphylosoff

hey

the first line in one of my subroutine is
shift;

but in this case i cannot use $_ and think $_ = shift;
why ?

entire code:

my @gilligan = qw(a b c d e);
my @skipper = qw(1 2 3 4 5);
my @professor = qw(I II III IV V);
my %all = (
Gilligan => \@gilligan,
Skipper => \@skipper,
Professor => \@professor,
);

sub check_items_for_all {
# $_ = shift; # try this
shift;
for $key (keys %$_) {
check_required_items($key, $_->{$key});
}
}

sub check_required_items {
my $who = shift;
my $items = shift;

my @required = qw(1 2 3 I II III a b c d V);
my @missing = ( );

for my $item (@required) {
unless (grep $item eq $_, @$items) { # not found in list?
print "$who is missing $item.\n";
push @missing, $item;
}
}

if (@missing) {
print "Adding @missing to @$items for $who.\n";
push @$items, @missing;
}
}

Re: what "shift" does, if not "$_ = shift;" ?

am 29.11.2007 19:35:00 von Peter Makholm

devphylosoff writes:

> the first line in one of my subroutine is
> shift;

This will just discard the first argument from @_. Not really usefull
in you functions as you don't use @_ later on.

> but in this case i cannot use $_ and think $_ = shift;
> why ?

If you think that 'shift' in void context is equal to '$_ = shift'
then you thinking is wrong. Why you think that I'm not able to tell,
the documentation for the shift function doesn't seem to imply this.

The documentation says that shift works on the @_ array if the
argument to shift is omitted. But it doesn't say anything about shift
doing other special things.


> entire code:

If this is you entire code, then I have no idea what you expect. You
define some variables and two functions, but use neiter.

//Makholm

Re: what "shift" does, if not "$_ = shift;" ?

am 29.11.2007 19:39:12 von devphylosoff

sorry, I lose

print map {" @{$_} \n"} values %all;
check_items_for_all(\%all);
print map {" @{$_} \n"} values %all;

in my pasted code

Re: what "shift" does, if not "$_ = shift;" ?

am 03.12.2007 02:05:25 von Joe Smith

devphylosoff wrote:

> print map {" @{$_} \n"} values %all;
> check_items_for_all(\%all);
> print map {" @{$_} \n"} values %all;

So, you're expecting $_ to be unchanged after calling check_items_for_all().
It won't be, based on the code you commented out.

You'd need
local $_ = shift;

Or better yet, use a private lexical variable instead of the global $_ variable.
my $href = shift;
for $key (keys %$href) {
check_required_items($key, $href->{$key});
}

-Joe

Re: what "shift" does, if not "$_ = shift;" ?

am 04.12.2007 01:27:08 von Michele Dondi

On Sun, 02 Dec 2007 17:05:25 -0800, Joe Smith wrote:

>You'd need
> local $_ = shift;
>
>Or better yet, use a private lexical variable instead of the global $_ variable.
> my $href = shift;
> for $key (keys %$href) {
> check_required_items($key, $href->{$key});
> }

No that 5.10 (Beta) is out, and if one wants to rely on $_ being the
topicalizer, then (s)he can use a lexical one too:

my $_=shift;

Of course, in the example above it is of no usefulness.


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,