Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: newbie having syntax/variable problems

by bikeNomad (Priest)
on Aug 07, 2001 at 08:23 UTC ( #102695=note: print w/replies, xml ) Need Help??


in reply to newbie having syntax/variable problems

I'm guessing you meant to type $_[KERNEL] etc. but the formatter ate it.

These are references to members in the array named @_ , which is how arguments get passed to subroutines in Perl. I'm guessing that KERNEL and HEAP are defined as constants (or are otherwise subroutines), and that they return numbers that can be used to index into @_. Why would someone get to arguments this way? Perhaps they mean to modify one of their arguments.

shift is used to remove and return an element off the start of an array. It is often used in conjunction with @_ to get (copies of) subroutine arguments. That is, given the following:

use constant KERNEL => 0; sub sub1 { $_[KERNEL] = 3; # modifies caller's version of arg } sub sub2 { my $kernel = shift; # copies arg $kernel = 3; # does not modify caller's version }
The reference to @_ in sub1 allows you to change the caller's version of the subroutine argument. In sub2, the argument is copied, and so changes made later to $kernel (which is copied from $_[0] ) don't get made to the caller's version.

update: clarified which end that shift operates on.

Replies are listed 'Best First'.
Re: Re: newbie having syntax/variable problems
by dragonchild (Archbishop) on Aug 07, 2001 at 16:48 UTC
    Just one minor correction - shift takes off the beginning of an array. pop takes it off the end.

    ------
    /me wants to be the brightest bulb in the chandelier!

    Vote paco for President!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2021-05-12 05:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (124 votes). Check out past polls.

    Notices?