Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Not quite understanding a couple of things

by pikablu (Initiate)
on May 25, 2007 at 00:02 UTC ( #617364=perlquestion: print w/replies, xml ) Need Help??

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

I am not sure if I am posting in this in the right place, but here goes nothing. I do not understand what @_ and $_ do. I also do not quite understand how to work a hash.
  • Comment on Not quite understanding a couple of things

Replies are listed 'Best First'.
Re: Not quite understanding a couple of things
by liverpole (Monsignor) on May 25, 2007 at 00:16 UTC
    Hi pikablu,

    $_ is the default variable, when you don't supply one.  It can be used as the default to many functions, as well as the default value in numerous contexts, and is kind of similar to the English pronoun "it".

    For example, you could either do:

    foreach my $value ('1', '2', '3') { print "Next value is $value\n"; }

    Or you could do, instead:

    foreach ('1', '2', '3') { print "Next value is $_\n"; }

    In contrast, @_ is the default array.  One place where it's used quite extensively is in a subroutine, to refer to passed arguments.

    For example:

    sub do_something { my ($arg1, $arg2) = @_; printf "You passed arguments '%s' and '%s'\n", $arg1, $arg2; }

    To begin to get acquainted with hashes (and a lot more), please read perlintro for starters.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
      Thank you very much, that explained a lot to me. I get it now. @others: Yes, I am at the beginning of my PERL journey, and I thank everyone who posted.
        Thank you very much, that explained a lot to me. I get it now. @others: Yes, I am at the beginning of my PERL journey, and I thank everyone who posted.

        Lesson #1: it's not PERL, but either Perl or perl. See also PERL as shibboleth and the Perl community. However, to expand on the answers you got thus far: yes, $_ and @_ are special variables, described along with all the other special vars in perldoc perlvar. While it is true that the former is the default scalar variable in the sense that many constructs default to it, I wouldn't go so far as to say that the latter is the "default array": in fact it is much a technical device for parameter passing in subs, which in Perl 5 is in fact both extremely simple and has far reaching consequences. In Perl 6 by contrast @_ will still be there but thanks to the much more complex and powerful mechanism for parameter passing, you won't need most of the time. Back to 5, you can still use @_ as a generic array, but as someone said, this kind things smells. One situation in which you may want to assign to @_ is when you want to use magic goto (BTW: don't you ever use non-magic one), but that is an advanced topic that I would postpone to a much later lesson.

        In your OP you also write "I also do not quite understand how to work a hash." Well, this is a very basic Perl topic covered in the first few chapters or pages of any introductory book or tutorial. So I'm not even trying to explain to you how to work with a hash in Perl in any detail, because for sure there are lots of people already doing that far better than I ever could. I'll just tell you what a hash is: a mapping from some finite set (of strings) into some other (also finite, for obvious reasons) set, implemented in such a way that lookup is generally quite fast (but generally you don't care at all).

Re: Not quite understanding a couple of things
by Joost (Canon) on May 25, 2007 at 00:50 UTC
    All the documents linked below should also be available on your system (provided you've got a full perl base install) using "perldoc NAME" (for full manual sections) or "perldoc -f NAME" (for functions) from the command line, where NAME is the text of the link. See also "perldoc perltoc"

    How to use hashes (and arrays and scalars), see perldata.

    @_ and $_ are the default array and scalar. For instance shift and pop without arguments modify @_, and print without arguments prints the current value of $_.

    @_ also gets set with the arguments to the current subroutine and $_ is used as a general single argument. See perlsub and perlvar.

Re: Not quite understanding a couple of things
by GrandFather (Saint) on May 25, 2007 at 00:16 UTC
Re: Not quite understanding a couple of things
by shmem (Chancellor) on May 25, 2007 at 06:08 UTC
    Just to add another great resource regarding $_ - see Builtin functions defaulting to $_.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2022-05-18 07:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (68 votes). Check out past polls.

    Notices?