Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Can't understand one line

by anaconda_wly (Scribe)
on Jan 07, 2013 at 06:58 UTC ( #1011960=perlquestion: print w/ replies, xml ) Need Help??
anaconda_wly has asked for the wisdom of the Perl Monks concerning the following question:

What does this mean? thanks!  $registry->$_( shift(@_) )
shift(@_) is a current $_, so what " $registry->$_( $_ ) " means? From Win32::TieRegistry.pm sub import

Comment on Can't understand one line
Download Code
Re: Can't understand one line
by BrowserUk (Pope) on Jan 07, 2013 at 07:16 UTC

    $registry->$_( shift(@_) ) ^^^^^^^^^^^ ^ ^............invoke a method ^^ ............the name of which is stored in $ +_ ^^^^^^^^^^ ............passing the next value from @_ a +s an argument.

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      OK, but what is the sequence? "shift(@_) "first , or $_ first? in
      $registry->$_( shift(@_) )
      I guess shift(@_) must be first, or else meaningless. Thanks!
        ... shift(@_) must be first ...

        Quite right.

Re: Can't understand one line
by Athanasius (Monsignor) on Jan 07, 2013 at 08:09 UTC
    shift(@_) is a current $_

    No. Here is the skeleton of the opening of the import subroutine in Win32::TieRegistry:

    sub import { my $pkg = shift(@_); ... local( $_ ); while( @_ ) { $_= shift(@_); if( ... ) { ... } elsif( ... ) { ... ... } elsif( exists $_opt_subs{$_} ) { ... $registry->$_( shift(@_) ); } elsif( ... ) { ...

    Each call to shift removes another element from the head of the @_ array, i.e., another argument from the list of those passed into sub import. So in the line:

    $registry->$_( shift(@_) );

    the variable $_ holds the last argument to have been removed from @_; but the expression shift(@_) removes the next argument from @_. $_ and shift(@_) will (most likely) have different values.

    shift(@_) does not reset the value of $_:

    17:43 >perl -wE "f(1, 2, 3); sub f { local($_) = shift(@_); my $c = sh +ift(@_); say '$_ = ', $_; say '$c = ', $c; say '@_ = ', @_; }" $_ = 1 $c = 2 @_ = 3 18:07 >

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      That' great!Thanks!
Re: Can't understand one line
by CountZero (Bishop) on Jan 07, 2013 at 09:06 UTC
    What makes you think that "shift(@_) is a current $_"?

    They are totally separate variables.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: Can't understand one line
by 7stud (Deacon) on Jan 07, 2013 at 09:43 UTC
    use strict; use warnings; use 5.012; package Person; sub new { my $class = shift; my $self = {}; bless $self, $class; return $self; } sub do_this { my($self, $greetee) = @_; say "hello $greetee"; } sub do_that { my($self, $greetee) = @_; say "goodbye $greetee"; } 1;
    use strict; use warnings; use 5.012; use Person; my $obj = Person->new; $obj->do_this("Sam"); $obj->do_that("Cathy"); say '*' x 20; my @func_names = ('do_this', 'do_that'); my @names = ("Sam", "Cathy"); call_funcs(@names); sub call_funcs { for (@func_names) { $obj->$_(shift @_); } } --output:-- hello Sam goodbye Cathy ******************** hello Sam goodbye Cathy

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (11)
As of 2014-10-24 15:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (132 votes), past polls