meonkeys has asked for the wisdom of the Perl Monks concerning the following question:

sub a { my %conf = ( blah => 1200 ); $conf{ shift }; } print a('blah');

I'm just curious why this sub doesn't print "1200". Does the shift fail because it's in a different lexical scope than the enclosing subroutine?

Replies are listed 'Best First'.
Re: silly question about shift
by japhy (Canon) on Aug 31, 2001 at 04:40 UTC
    Any bareword found inside {...} is taken as a single-quoted string. You must make shift not a bareword.
    $conf{+shift} $conf{shift()} $conf{shift @_}

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

(Ovid) Re: silly question about shift
by Ovid (Cardinal) on Aug 31, 2001 at 04:42 UTC

    Because it's looking for a hash element named 'shift'. Try the following:

    sub a { my %conf = ( blah => 1200 ); $conf{ +shift }; } print a('blah');

    Cheers,
    Ovid

    Vote for paco!

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: silly question about shift
by Anonymous Monk on Sep 01, 2001 at 08:09 UTC
    1 you don't return anything from the function. 2 shift needs an array; $hash{} needs a key; obviously not going to work