http://qs1969.pair.com?node_id=726403

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.

Replies are listed 'Best First'.
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.

Re: shift doesnt shift
by ForgotPasswordAgain (Priest) 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 mje (Curate) 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 zentara (Archbishop) on Nov 27, 2008 at 13:42 UTC
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 :-))