Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

(Perl 5.10.1 or before)What does $_ refer to here?

by hghosh (Acolyte)
on Jun 10, 2019 at 00:35 UTC ( #11101170=perlquestion: print w/replies, xml ) Need Help??

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

Hello, Perl Monks. I'd like to know what $_ refers to in line 6. This script was written in 2010 by someone else who didn't use "use warnings" or "use strict." I'm aware split() no longer works in a void context. I'd like to know which value the  $_ refers to. Does it refer to what is later called $line ? Does it still mean the same thing in the second, strict version?

#var $cutoff defined earlier in script open IN, "$filename.merged"; while (<IN>) {chomp; split(/\t/); if ($_[1] >= $cutoff){ $_[0] =~/(chr.+):(\d+)-(\d+)/; foreach ($2..$3) {$breakpoint{$1}{$_}=1;} #what is $_? } }
Here is my "strictured" version:
open IN, "$filename.merged"; while (<IN>) { chomp(my $line= $_); my @array = split(/\t/, $line); if ($array[1] >= $cutoff){ # cutoff is defined earlier in original + script $array[0] =~/(chr.+):(\d+)-(\d+)/; foreach ($2..$3) {$breakpoint{$1}{$_}=1;} #would it be $line?

Replies are listed 'Best First'.
Re: (Perl 5.10.1 or before)What does $_ refer to here?
by LanX (Archbishop) on Jun 10, 2019 at 00:46 UTC
    Hi

    foreach ($2..$3) {$breakpoint{$1}{$_}=1;}

    Thats the loop var of the foreach.

    In this case the regex matches 2 and 3 from the previous line.

    If you wanna get rid of it, try:

    for my $m ($2..$3) {$breakpoint{$1}{$m}=1;} *

    As a side note: Your declaration of $line looks weird, why not directly inside the while condition?

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

    Update

    *) From perlsyn

    The foreach keyword is actually a synonym for the for keyword, so you can use either. If VAR is omitted, $_ is set to each value.

      how would I use $line directly in the while condition? And what makes that better than declaring it as I did? Also, if I left $_ as it is in the second, strictured code block, it would refer to the same $_ as the first block of code, right? Because you're saying that the $_ refers to the iterator in "foreach". I apologize for parroting back what you're saying. I'm a perl novice, and this helps me solidify information.

        Hello hghosh,

        how would I use $line directly in the while condition? And what makes that better than declaring it as I did?

        I think LanX is recommending something along these lines:

        while (my $line = <IN>) { chomp $line; ...

        which is simpler and clearer.

        Also, if I left $_ as it is in the second, strictured code block, it would refer to the same $_ as the first block of code, right?

        Not sure which blocks you’re referring to; but, just to be clear, $_ refers to the “current” (i.e., the most closely related) syntactic construct that sets it. For example:

        use strict; use warnings; for ('A' .. 'C') { print "Outer: \$_ = $_\n"; for (10 .. 12) { print " Inner: \$_ = $_\n"; } print "Outer: \$_ = $_\n"; }

        produces:

        23:21 >perl 2004_SoPW.pl Outer: $_ = A Inner: $_ = 10 Inner: $_ = 11 Inner: $_ = 12 Outer: $_ = A Outer: $_ = B Inner: $_ = 10 Inner: $_ = 11 Inner: $_ = 12 Outer: $_ = B Outer: $_ = C Inner: $_ = 10 Inner: $_ = 11 Inner: $_ = 12 Outer: $_ = C 23:22 >

        — showing that there are actually as many independent instances of $_ (two, in this case) as the scoping requires.

        Hope that helps,

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

        Hi

        C&P on googling "default variable Perl":

        Perl has something similar to the pronoun "it" a default variable that is the implied target of many operators and built-in functions in Perl. If you do not specify a variable, the functions and operators are executed on the default variable. That variable is $_, and it is the single most important variable in Perl.

        So it depends on the last relevant statement.

        Because this can get quickly messy with longer code it's good style to avoid it then by using explicit variables.

        You could write while ( my $line = <IN> ) { ...

        Hope it's clearer now :)

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

        Update

        See also Gabor's in depth explanation

        https://perlmaven.com/the-default-variable-of-perl

Re: (Perl 5.10.1 or before)What does $_ refer to here?
by ForgotPasswordAgain (Curate) on Jun 13, 2019 at 13:41 UTC
    Unsolicited advice, but since you're "strictifying" the script, I'd recommend changing open IN, $filename to open my $in, '<', $filename (and if you don't use autodie, then also add or die("Couldn't open $filename: $!")). A global FILEHANDLE led to one of the nastiest bugs I've ever debugged. Also the code assumes $1, $2, and $3 were matched, so you might want to add a check on whether they're defined.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2019-06-27 02:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Is there a future for codeless software?



    Results (111 votes). Check out past polls.

    Notices?