Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

uninitialized $_ sneaks past warnings in grep/map block or expression

by rir (Vicar)
on Sep 01, 2010 at 13:36 UTC ( [id://858335]=perlquestion: print w/replies, xml ) Need Help??

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

Update: I'd downvote this if I could. My error was thinking $a and $b when $_ was appropriate.

The recent thread, Better algorithm or data structure?, shows that this behavior of $_ is not well known. It surprised me. It took prompting for this aspect to register (thanks, BrowserUk).

Update: reduced code to the point. This expresses that to which I was referring. Map and grep, like for can, localize $_; this makes this post nonsensical;

## perl 5.10.1 use strict; use warnings; my $oo; my @ary = ( 0, 1, 2, 3); grep $ary[$_ ], @ary; # no warning grep $ary[$oo], @ary; # warns
Be well,
rir

Original verbose code:

Replies are listed 'Best First'.
Re: uninitialized $_ sneaks past warnings in grep/map block or expression
by merlyn (Sage) on Sep 01, 2010 at 13:39 UTC
    If you understand the rule that it's ok to copy undef to another variable, but not use it in an operation, all of these make perfect sense.

    -- Randal L. Schwartz, Perl hacker

    The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

      That explains the first, but undef isn't being copied in the map and grep statements. The OP doesn't know that map and grep set $_.

Re: uninitialized $_ sneaks past warnings in grep/map block or expression
by AnomalousMonk (Archbishop) on Sep 01, 2010 at 14:01 UTC

    rir: Do you understand that  $_ is 'topicalized' within the body of  grep map for (and maybe one or two more I've forgotten) statements?

    >perl -wMstrict -le "print $_; my @ra = (0, 1, 2, 3); print grep { print qq{topicalized \$_: '$_'}; $ra[$_] } @ra; print $_; " Use of uninitialized value $_ in print at -e line 1. topicalized $_: '0' topicalized $_: '1' topicalized $_: '2' topicalized $_: '3' 123 Use of uninitialized value $_ in print at -e line 1.
Re: uninitialized $_ sneaks past warnings in grep/map block or expression
by mr_mischief (Monsignor) on Sep 01, 2010 at 14:09 UTC
    Are you sure it's not documented in perlvar? The entry for $_ states when it is used as an iterator or the destination of an input record for a while loop. If it's not defined, it's not defined. If it is being given a value by a for loop, a while loop with a readline operator, given, grep, or map, then it is defined.
      If it is being given a value by a for loop, a while loop with a readline operator, given, grep, or map, then it is defined.
      Not if the value given isn't defined.
      $ perl -wE 'say for 1, undef, 3' 1 Use of uninitialized value in say at -e line 1. 3

        That's a trick. You've very specifically given it a non-value. ;-)

Log In?
Username:
Password:

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

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

    No recent polls found