Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Simple Noob Question

by jdlev (Scribe)
on Mar 04, 2011 at 05:23 UTC ( #891357=perlquestion: print w/ replies, xml ) Need Help??
jdlev has asked for the wisdom of the Perl Monks concerning the following question:

! defined and $_ = ' ' for @$row;

I was wondering if some monks could explain the above statement to me. A few things are confusing.

1) I know ! means "no", so does the first part mean 'not defined'?

2) I've never understood what this means '$_', and if I'm not mistaken you can also use the underscore with arrays & hashes like '@_' and '%_'?

3) How can you set an array and a variable at the same time? In other words, what does @$row mean or what is it accomplishing?

Thanks monks! OOOOMMMM!!

Comment on Simple Noob Question
Re: Simple Noob Question
by Anonymous Monk on Mar 04, 2011 at 05:31 UTC
    B::Deparse, use it
    $ cat junk.pl ! defined and $_ = ' ' for @$row; $ perl -MO=Deparse,-p junk.pl ; (defined($_) or ($_ = ' ')) foreach (@$row); junk.pl syntax OK

    I've never understood what this means '$_',

    Then you haven't read perlintro, go read it now, and try out the examples.

    Also see perlvar, it also explains $_

    In other words, what does @$row mean or what is it accomplishing?

    It is de-referencing, see references quick reference, basically

    my @array = 1 ..2; print "@array\n"; my $ref = \@array; print "@$ref\n"; __END__ 1 2 1 2
Re: Simple Noob Question
by jwkrahn (Monsignor) on Mar 04, 2011 at 05:38 UTC
    ! defined and $_ = ' ' for @$row;

    Could also be written as:

    not defined( $_ ) and $_ = ' ' for @$row;

    Or more simply in modern versions of Perl:

    $_ //= ' ' for @$row;

    1) ! means "not" as in the not operator (see example above.)

    2) You can also use it for filehandles '_' and subroutines '&_'.    It is just another valid character that can be used as variable names (see perlvar.)

    3) $row contains a reference to an array and to dereference the array you put a '@' in front of $row.

Re: Simple Noob Question
by ikegami (Pope) on Mar 04, 2011 at 05:38 UTC
    • Foreach loops alias $_ to each element of the list in turn (unless told to use a different variable). Any change to $_ will change the element to which its aliased.
    • and short-circuits, meaning it will only evaluate its RHS if its LHS is true.
    • defined with no argument is defined($_).
    • @$row is an array dereference. It means "the array referenced by $row".

    These all do the same:

    !defined and $_ = ' ' for @$row;
    for (@$row) { !defined and $_ = ' '; }
    for (@$row) { if (!defined) { $_ = ' '; } }
    for $_ (@$row) { if (!defined($_) { $_ = ' '; } }
    for my $i (0..$#$row) { if (!defined($row->[$i]) { $row->[$i] = ' '; } }

    In 5.10 and above, that could have been written

    $_ //= '' for @$row;
Re: Simple Noob Question
by sundialsvc4 (Abbot) on Mar 04, 2011 at 14:02 UTC

    Unfortunately for you, you’ve stumbled into a bit of “Perl golf.”   Such code is easy-to-read for the initiated, but quite confusing otherwise.   I routinely re-write such code, or at least add comments directly above it.   Your questions are entirely reasonable... because the code, as written, doesn’t expressly tell you.

    Perl has many “predefined variables,” and it allows you to write code that omits any explicit reference to any variable, in which case the code is understood by Perl to implicitly reference certain of those variables.   One of those variables is $_, which is, basically, “the last thing that was read or referenced.”   (See the perldoc topics previously referred-to in other replies.)

    Further complicating the situation is Perl’s use of “contexts.” In other words, it means something when you use $ vs. @ vs. %, and this meaning is sometimes quite arcane unintuitive.

    Mind you, “I come neither to praise Caesar, nor to bury him,” but merely to describe him.   My comments here are merely meant to be “factual, descriptive, and neutral.”   You will encounter code like this, and in time, you may find yourself writing code like this, and “so be it.”   Certain aspects of the Perl language rely upon knowledge of the subtleties of the language ... subtleties which are, some might argue (and some might dispute) are the language’s greatest strengths.   It does bring you a bit of a learning curve that you might not have expected, and a bit more of a learning curve than you might have wished for.   But these things, too, shall pass.

    Welcome to the Monastery, and to the Perl community.   Come here often.   Ask away.

      Well, to the uninitiated, everything is magic

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2014-12-25 20:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (163 votes), past polls