Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: how to call a sub via variable in library

by BrowserUk (Patriarch)
on Nov 17, 2014 at 19:23 UTC ( [id://1107462]=note: print w/replies, xml ) Need Help??


in reply to how to call a sub via variable in library

I've been staring at this unholy construct $self->{&{\&{$testname}}}->($system); for about 15 minutes, And I still haven't a clue what it's meant to do.

Perhaps you could fill in the blanks?

  1. $testname is a string containing the name of a subroutine?
  2. \&{ $testname }; returns a code reference to the named subroutine.
  3. &{ \&{ $testname } } invokes that coderef. Ie, a long winded and obfuscated way of doing: &$testname
  4. $self->{ &{ \&{ $testname } } } Uses the (if any) return code from the subroutine as a key into the object hash.
  5. $self->{ &{ \&{ $testname } } }->( ... ); who's associated value is another coderef, which it invokes ...
  6. $self->{ &{ \&{ $testname } } }->( $system ); passing one variable $system (which is apparently a hashref.)

Unless you have pre-populated the object hash with a key matching every possible return value from every possible test name, and associated an appropriate coderef for all of those keys; that line isn't trying to do what you think it is. (And even if it is, the way it is written is unnecessarily complicated and obfuscate.)

(And a brief glance at the rest of your code makes me think you're writing code you will not be able to maintain.)


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.

Replies are listed 'Best First'.
Re^2: how to call a sub via variable in library
by Anonymous Monk on Nov 17, 2014 at 19:33 UTC
    I've been staring at this unholy construct $self->{&{\&{$testname}}}->($system); for about 15 minutes, And I still haven't a clue what it's meant to do.
    This is a very obscure and useless feature of Perl. Documented in strict
    $bar = \&{'foo'}; &$bar;
    is allowed so that "goto &$AUTOLOAD" would not break under stricture.

      That bit is obfuscated, but at least understandable.

      It's the invoking of a subroutine from the object hash keyed by the return code of that subroutine that I find completely unlikely.


      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.
Re^2: how to call a sub via variable in library
by glenn (Scribe) on Nov 17, 2014 at 21:18 UTC
    you have given the best understanding of which i did not even have... Although this is wrong here is what i want to accomplish: $self->&$testname($system). Execute sub '$testname' with '$system' as parameter in object '$self'. Could you help further with this???
      here is what i want to accomplish: $self->&$testname($system). Execute sub '$testname' with '$system' as parameter in object '$self'.

      Depends what do you mean by "... in object $self"?

      I see two possible interpretations of that:

      1. You simply want to execute the sub named in $testname (whilst incidentally running inside a method of object $self); in which case all you need is:
        $testname->( $system );

        You'll probably need no strict 'refs'; for that.

      2. Alternatively, you might mean that you want to invoke the method of object $self, who's name is held in $testname; in which case all you need is:
        $self->$testname( $system );

        Seems to work fine with full strict.


      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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-25 07:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found