Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

What does @_[OBJECT, ARG0, ARG1] mean?

by neilwatson (Priest)
on Feb 27, 2016 at 21:31 UTC ( #1156337=perlquestion: print w/replies, xml ) Need Help??

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

Consider this sub from Bot::BasicBot

sub _fork_said { my ($self, $body, $wheel_id) = @_[OBJECT, ARG0, ARG1]; chomp $body; # remove newline necessary to move data; # pick up the default arguments we squirreled away earlier my $args = $self->{forks}{$wheel_id}{args}; $args->{body} = $body; $self->say($args); return; }

What does the first line in the sub do? It would seem to me to set off a strict error. Original code here.

Neil Watson
watson-wilson.ca

Replies are listed 'Best First'.
Re: What does @_[OBJECT, ARG0, ARG1] mean?
by toolic (Bishop) on Feb 27, 2016 at 21:47 UTC
    My guess is that one of the modules it uses exports three constants, like:
    use constant { OBJECT => 0, ARG0 => 1, ARG1 => 2, };

    Then, your line is essentially like:

    my ($self, $body, $wheel_id) = @_[0, 1, 2];

    UPDATE: see POE::Session

Re: What does @_[OBJECT, ARG0, ARG1] mean?
by AnomalousMonk (Chancellor) on Feb 27, 2016 at 22:08 UTC

    Further to toolic's reply: the expression  @_[0, 1, 2] is an array slice (see Slices in perldata). Don't be thrown by the  _ (underscore): that's a perfectly valid (special) variable name.


    Give a man a fish:  <%-{-{-{-<

Re: What does @_[OBJECT, ARG0, ARG1] mean?
by thomas895 (Chaplain) on Feb 28, 2016 at 00:53 UTC

    When you encounter things like this in the future, the Debugger is very useful. In particular, look at the X vars... (or V Package::Name vars...) commands. Try, for example,

    DB<1> X OBJECT $OBJECT = 123

    There's nothing special about constants. All that constant.pm does is create a method in the package that returns the value you gave, by cleverly using import through use. Here's the relevant parts from constant.pm in 5.12.3:

    #===================================================================== +== # import() - import symbols into user's namespace # # What we actually do is define a function in the caller's namespace # which returns the value. The function we create will normally # be inlined as a constant, thereby avoiding further sub calling # overhead. #===================================================================== +== sub import { #... #note: $full_name is set to "Package_I_Was_Called_From::ConstantNa +me", for each constant you provide #if you have a scalar, it basically does *$full_name = sub () { $scalar }; #and if you have an array: *$full_name = sub () { @list }; #... }

    Note: In the debugger output above, you may have noticed that it appeared as a regular variable. Indeed, in Perl 5.8 (as I understand it), constant.pm had some magic added to it so that scalars are simply added into the symbol table, appearing to be a package variable. Still, you can't really use them as variables, as you can observe here:

    DB<6> x &OBJECT 0 123 DB<7> x $OBJECT 0 undef
    -Thomas
    "Excuse me for butting in, but I'm interrupt-driven..."

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2019-06-24 09:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Is there a future for codeless software?



    Results (97 votes). Check out past polls.

    Notices?