Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Function name in a variable, can't recall the concept

by mpersico (Monk)
on Apr 02, 2019 at 15:00 UTC ( [id://1232020]=note: print w/replies, xml ) Need Help??


in reply to Function name in a variable, can't recall the concept

Today is not my day. When it was mentioned that what I demonstrated in my examples wouldn't work under use strict; I went back and looked at the code again and realized my example was wrong. Here's what I am really doing:
my $funcname = ( $someCondition ? 'foo' : 'bar' ); $someObject->$funcname($arg1, $arg2);
It's not "Function name in variable", it's "Method name in variable", and a relevant reference to same is https://perldoc.perl.org/perlobj.html#Method-Call-Variations

Replies are listed 'Best First'.
Re^2: Function name in a variable, can't recall the concept
by VinsWorldcom (Prior) on Apr 02, 2019 at 21:28 UTC

    That's OK, from examples above:

    use strict; use warnings; package Object; sub new { return bless \{} } sub foo { printf "hello foo = %s\n", ( caller(0) )[3]; } sub bar { printf "hello bar = %s\n", ( caller(0) )[3]; } package main; my $someObject = Object->new(); my $someCondition = 0; my $funcname = ( $someCondition ? 'foo' : 'bar' ); $someObject->$funcname($funcname); $someCondition = 1; $funcname = ( $someCondition ? 'foo' : 'bar' ); $someObject->$funcname($funcname);

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-20 01:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found