Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

newbie having syntax/variable problems

by kvaishnav (Initiate)
on Aug 07, 2001 at 08:04 UTC ( [id://102690]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

I was looking thru the perl code which is already written. Could anyone tell me what exactly the following means: $_[KERNEL] and $_[HEAP] and shift

Thanks

Replies are listed 'Best First'.
Re: newbie having syntax/variable problems
by mattr (Curate) on Aug 07, 2001 at 11:39 UTC
    Actually these probably refer to POE, a perl application kernel (it stands for "Persistent Object Environment") described here and here (easier).

    It lets you build event-driven apps in Perl. Here is a neat paragraph about it.

    The two special constants you mention are exported by POE and are references that let you access parts of what is a little Perl-based operating system for your application. For example the heap dataspace can be used to store information that you don't want to have Perl garbage collect away, such as the address and port of a client.

    In the words of the POE manpage,

    # $_[KERNEL] is a reference to the program's global POE::Kernel # instance; $_[HEAP] is the session's local storage; $_[SESSION] is # a reference to the session itself.
    You can use this information with object methods, so
    my $kernel = $_[KERNEL]; $kernel->run;
    will start up the event loop and return when there are no more sessions to which events may be dispatched. The POE::KERNEL manpage has a list of such methods.
Re: newbie having syntax/variable problems
by bikeNomad (Priest) on Aug 07, 2001 at 08:23 UTC
    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.

      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
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://102690]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-04-25 06:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found