Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

shift doesnt shift

by joec_ (Scribe)
on Nov 27, 2008 at 13:31 UTC ( #726403=perlquestion: print w/ replies, xml ) Need Help??
joec_ has asked for the wisdom of the Perl Monks concerning the following question:

Hi, am i missing something? I have this code:

$molfile=shift || die ('script takes one arg from CL');

And when i call it like

./service.pl file.txt

I always get the die error message displayed. I have to explicitly say

$molfile = $ARGV[0] || die.....;

to get it to work.

This is a bit strange as i have the same code in a different file that works...

Any thoughts?

Thanks
Joe.

Comment on shift doesnt shift
Select or Download Code
Re: shift doesnt shift
by ForgotPasswordAgain (Deacon) on Nov 27, 2008 at 13:42 UTC

    Need to debug a bit. What's "$@" that you're shifting? What's the value being shifted off?

    UPDATE: sorry, I meant @_, not $@ ....

Re: shift doesnt shift
by zentara (Archbishop) on Nov 27, 2008 at 13:42 UTC
Re: shift doesnt shift
by mje (Deacon) on Nov 27, 2008 at 13:43 UTC

    works for me so long as the argument is true i.e.,

    ./service.pl file.txt

    works but of course the following do not:

    ./service.pl '' ./service.pl 0

    for the obvious reason that the empty string and 0 are not true.

      $@ is empty. Im just using the name of the command line arg to read in a file of data. Replacing || with or didnt work. Like i said, the same code works in a different script. What else could cause a shift to die?

      TIA. Joe.

        We need to see more code.

        You wrote: X or Y
        1) Y is evaluated when X is false
        2) X is empty (aka undef, aka false)
        1+2=3) Therefore Y (the die) is performed.

        So, what is the problem here? It is doing exactly what you told it to. If you want it to die on some other condition than the shift giving 0/undef/''/etc, then you need to specify that condition in your code.

        Try specifying what to shift: $var = shift @array to ensure you're shifting the right array.
Re: shift doesnt shift
by Bloodnok (Vicar) on Nov 27, 2008 at 13:59 UTC
    From what little you've given us, I suspect the explanation/root of the problem is the context...so, as Corion says, we ned more code - I'd suggest contextual examples from both working and non-working instances.

    A user level that continues to overstate my experience :-))
Re: shift doesnt shift
by jplindstrom (Monsignor) on Nov 27, 2008 at 13:59 UTC
    Is the shift statement inside a subroutine or block of any kind?

    If it is, it's shifting off @_ instead of @ARGV.

    perldoc -f shift ... If ARRAY is omitted, shifts the @_ array within the lexical scope of subroutines and formats, and the @ARGV array at file scopes ...

    /J

      That was it. It was shifting off @_. Thanks.

        Sometimes it pays not to depend on the default targets.

        I've banged my head on similar problems just because I didn't know or misunderstood what was being read.

        So now I always use whatever is needed explicitly, e.g., shift(@ARGV). But that's just me because I basicly suck at programming ;). Not a pro by any means.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://726403]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2014-07-26 02:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls