Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Not quite understanding a couple of things

by liverpole (Monsignor)
on May 25, 2007 at 00:16 UTC ( [id://617365]=note: print w/replies, xml ) Need Help??


in reply to Not quite understanding a couple of things

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$..$/

Replies are listed 'Best First'.
Re^2: Not quite understanding a couple of things
by pikablu (Initiate) on May 25, 2007 at 01:51 UTC
    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).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-03-29 11:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found