Using $_

Hi,

I know the $_ operator is some global general storage for 'stuff' in certain operations, like:

    while (<>) {
         chomp;
    }

that is reading a line from input into $_ then runs chomp() on the contents of $_.

Is $_ used to get data passed to functions? In the following:

    sub whatever()
    {
         my ($x, @y, %z) = @_;
    }

is the $_ variable being used as a temporary storage area until you extract the passed parameters in the sub routine? If not, how do they get there!


Also, I know $_ is used here:

    for (@myarray) {
        print("Current element is: ", $_, "\n");
    }

is this typical in perl, or are people more likely to explicitly do something like:

    for (my $i = 0; $i < @myarray; $i++) {
        ....
    }

Thanks

DJ_AM_JuiceboxAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Adam314Connect With a Mentor Commented:
A prototype isn't necessary.  To declare a subroutine with a prototype, you leave off the parenthesis:
    sub test    #No parenthesis
    {
        my $x = $_[0];
        my $y = $_[1];
    }

You just pile on the type.  The problem with $@@$% for a prototype is that when parameters are passed, they are passed as a single list.  So when you are getting them back into their variables, you won't know when the second array ends and the third starts.  If you need to pass multiple arrays, you would usually use array references, with a prototype like $\@\@$\%.

For the subroutines: @_ is the array that holds all of the parameters passed to the subroutine.  It is an array.  Just like if you have an array @myArray, you access the elements like $myArray[0] and $myArray[1], you access the elements of @_ with $_[0] and $_[1].  This has nothing to do with $_, these are seperate unrelated variables.  When you use shift without any parameters, it shifts the @_ array.  So these two are exactly the same:
    my $x = shift;
    my $x = shift @_;
http://perldoc.perl.org/functions/shift.html

0
 
ozoCommented:
> Is $_ used to get data passed to functions?
not unless you explicitly use it with something like
($_,@y) = @_;

>    for (@myarray)
is typical perl
> for (my $i = 0; $i < @myarray; $i++) {
might be used by someone used to C, or if $i was going to be used for other things
0
 
DJ_AM_JuiceboxAuthor Commented:
Ok so in this statement:

    sub whatever()
    {
         my ($x, @y, %z) = @_;
    }

$_ is NOT used? What mechanism does perl use then to store the junk you passed the function when you called it over and into the whatever() function? Just curious.

Thanks
0
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

 
ozoCommented:
well, the prototype () says that whatever has no arguments,
but in general, arguments are passed in @_ , not $_

see
perldoc perlsub
0
 
Adam314Commented:
$_ and @_ are two different variables.  The $_ is a scalar, and @_ is an array.

Also, with:
    sub whatever()
    {
         my ($x, @y, %z) = @_;
    }

The @y would take up the all of the variables left, so there would be nothing in %z.
0
 
DJ_AM_JuiceboxAuthor Commented:
I'm confused - I thought a sub prototype didn't require any parameters, so you could write:


    test(5, 10);

    sub test()
    {
        my $x = $_[0];
        my $y = $_[1];
    }

and that works fine. So you're saying you could also write it like:

    sub test($$)
    {
        ..
    }

to mean it takes two scalars? You just pile the type one after another, like $@@$% means take a scalar, array, array, scalar, hash? (Not a curse word?).



Hmmm ok now about $_ from the docs:

"Any arguments passed in show up in the array @_ . Therefore, if you called a function with two arguments, those would be stored in $_[0]  and $_[1] ."

So, for whatever reason, in your sub body you can access the passed parameters like:

     my ($x, $y) = @_;

OR

   my $x = $_[0];
   my $y = $_[1];

OR
 
   my $x = shift;
   my $y = shift;


but I guess all three methods must use $_ because perl isn't compiled and it will only know which method you use at run-time right, so it needs to keep that inside $_ in case you want to use the latter two methods of accessing the parameters?

Thanks
0
 
DJ_AM_JuiceboxAuthor Commented:
For the $@@$ thing, I completely understand now, makes sense, thank you.

For the second part, I just find it so strange that they made this $_ variable which is used in for loops, regexes, etc, but then they also use the syntax $_[index] to get at the elements of @_, but the two are unrelated.

Thanks for your patience.
0
 
ozoCommented:
perldoc perldata
0
 
ozoCommented:
If it's not in a subroutine,
    my $x = shift;
is the same as
    my $x = shift @ARGV;
0
 
ozoCommented:
@_ is an array, hence @
$_[0] is a scalar, hence $
http://www.perlmonks.org/index.pl?node_id=861
0
All Courses

From novice to tech pro — start learning today.