Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Is this a numeric context thing?

by mephit (Scribe)
on May 17, 2002 at 03:58 UTC ( [id://167195]=perlquestion: print w/replies, xml ) Need Help??

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

Greetings, Monks.

I was skimming my older nodes and came across this reply, which says:

qw(foo bar))[$_[0]] would still yield 'foo' when @_ is empty (therefore, $_[0] is undef).

What I'm wondering, is if that's a numeric constant thing? If @_ is empty, does the array usage of [] force the undefined $_[0] into numeric context, which would make is a 0? Is that how that works? I did some tests, such as:

Enter h or `h h' for help, or `man perldebug' for more help. h main::(-:1): h DB<1> x $ARGV[0] 0 undef DB<2> x 3 + $ARGV[0] 0 3 DB<3> x 3 * $ARGV[0] 0 0
which is what I was expecting, thinking that an undef would be forced into a numeric 0 because of arithmetic operands. So, I'm pretty sure that's what's happening, but that's just an educated guess. Am I right?

Thanks.

--

Mephit (See my home node for my rant about Opera and PerlMonks, and my earliest nodes.

Replies are listed 'Best First'.
Re: Is this a numeric context thing?
by davorg (Chancellor) on May 17, 2002 at 07:01 UTC

    From perldoc perlsyn:

    A variable holds the undefined value (undef) until it has been assigned a defined value, which is anything other than undef. When used as a number, undef is treated as 0; when used as a string, it is treated the empty string, ""; and when used as a reference that isn't being assigned to, it is treated as an error. If you enable warnings, you'll be notified of an uninitialized value whenever you treat undef as a string or a number. Well, usually. Boolean ("don't-care") contexts and operators such as ++, --, +=, -=, and .= are always exempt from such warnings.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-19 23:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found