Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

shift, undef and 'or die'

by agoth (Chaplain)
on Jul 26, 2001 at 17:51 UTC ( [id://99973]=perlquestion: print w/replies, xml ) Need Help??

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

I'm trying to test for a parameter with shift or die.
The parameter in this case is zero, so I am wary of dodgy true/false interpretations on my part. However, with @_ being three items long, shift populates the variable it is supposed to and then dies as well.

Why?
Is it because shift is returning zero which is evaluating to false? I was expecting shift to return success if it could shift an item.
Is there an alternative operator I could use or some conbination of shift / defined?

#!/usr/local/bin/perl -w use strict; lookup(22); lookup(22, 'data'); my $var = 111; lookup(22, 'data', $var); $var = 0; lookup(22, 'data', $var); sub lookup { print "Num params", scalar (@_), "\n"; my $self = shift; my $data_type = shift or print "type not provided ConfReaderlookup +\n"; my $item = shift or print "item not provided ConfReaderlookup +\n"; print "ITEM isnt in here actually\n" unless defined $item; print "----------------------------\n"; }

Replies are listed 'Best First'.
Re: shift, undef and 'or die'
by arturo (Vicar) on Jul 26, 2001 at 18:01 UTC

    shift returns the value of the first entry in the array (or undef if there's nothing left in the array). So your surmise is right, if it's 0 it evaluates to false.

    use some variant on  my $data_type; print $errmsg unless defined ($data_type = shift); to get the behavior you want.

    update even that may not be totally correct; undef is a valid value for an entry in an array which may have more members -- the fact that shift returns undef does not guarantee that you've hit the end of the array -- so you may want to check both the size of the argument array and the definedness of its members, if your purposes require that.

    HTH.

    perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'
Re: shift, undef and 'or die'
by tadman (Prior) on Jul 26, 2001 at 18:15 UTC
    As arturo is suggesting, it might be more prudent to check parameters using defined rather than simple boolean math. It is more practical, as well, since you can really go to town:
    sub lookup { my $self = shift; my ($data_type,$item) = @_; warn "data_type not provided\n" unless (defined $data_type); warn "item not provided\n" unless (defined $item); # ... }
Re: shift, undef and 'or die'
by dragonchild (Archbishop) on Jul 26, 2001 at 18:07 UTC
    Is it because shift is returning zero which is evaluating to false?

    The short answer is "Yes". Shift returns what it shifts, not a success. Think about it this way - it can only really return one thing.

    To do this, you have to push it to a temp then do a trinary checking on defined-ness.

(tye)Re: shift, undef and 'or die'
by tye (Sage) on Jul 26, 2001 at 20:18 UTC
    die "Type not provided" unless @_; my $data_type= shift(@_);

    Is probably how I'd do it (though there are certainly other ways). (:

            - tye (but my friends call me "Tye")
Re: shift, undef and 'or die'
by ariels (Curate) on Jul 26, 2001 at 18:29 UTC
    Use the lvalue returned from the assignment, and test it for definedness:
    sub foo { defined(my $x = shift) or warn "No parameter given!" # ... }
    Of course, it still can't tell if the array was empty or if it shifted an undefined value off it.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-26 07:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found