I have a program where essentially the user is passing over the cli a subroutine they will want called. Lets say this is stored as:

my $userSub = "sub_they_call({param1=>'p'})";

what I'm getting at here is it's the EXACT sub call, options and all.

Now, if I just have some simple subroutine in my .pl file and I do:

&$userSub;

This seems to work for me.

HOWEVER, the issue is, I have scenarios where the sub needed to be called is an instance method for a particular class. So for example I'll have a class, and an instance of that class, and need to call the sub. example:

my $modObj = MyFoo->new(); #made my object #now MyFoo has a class called "get_it" my $str = "get_it()"; #if I do the following I get a syntax error $modObj->$str;

How is it that I can call the sub?

(By the way, I want to note this is a scenario where the subroutines is in the hundreds, possibly more, so I can't just store say a hash that has "directions" to call a certain sub based on certain string)

Re: [perlFun] Calling a subroutine whos name is stored as a scalar
[In reply to]

Can't Post

Quote

(By the way, I want to note this is a scenario where the subroutines is in the hundreds, possibly more, so I can't just store say a hash that has "directions" to call a certain sub based on certain string)

Re: [FishMonger] Calling a subroutine whos name is stored as a scalar
[In reply to]

Can't Post

I understand, but I'm sort of blocked from doing that in this case. I'm using some internal libraries my company owns that autogenerates these subroutines. There are potentially thousands of them and I don't even know what they all are.

about the eval, I'm not certain what to do with that. Are you saying if I put it in an eval it'll take away that syntax error I'm seeing and attempt to call the subroutine?

Re: [perlFun] Calling a subroutine whos name is stored as a scalar
[In reply to]

Can't Post

Quote

I'm using some internal libraries my company owns that autogenerates these subroutines. There are potentially thousands of them and I don't even know what they all are.

IMO, that indicates a very poor application design.

If the app autogenerates the subs, it could also add it to the dispatch table. When it needs to be executed, the app could first do a hash lookup to verify that the sub is available. If it is, then execute it, and if it isn't then a default error sub would be called.

Using eval to execute subs is fairly common, but I have not tested executing OO method calls. So, I can't say for sure if it will solve that part of your problem.

Re: [FishMonger] Calling a subroutine whos name is stored as a scalar
[In reply to]

Can't Post

In Reply To

Quote

IMO, that indicates a very poor application design.

Unfortunately I don't have access to the code from these modules, nor do I have permissions to change them. There is no dispatch table available to list all the subroutines. To make things extra fun they randomly decide to add in new subs/take them out, without telling anyone. So it's very dynamic and mostly a guessing game.

I guess my issue is, this is the situation I have to deal with. People are going to pass me over the cli, subroutines they want called. I can get those subs as scalars, and then I need to call them. That's pretty much all I have to work with.

One other option I have is I could write a script to go in and grab their code myself and find what all subroutines are available and then populate the hash table myself, but I've got to be honest I'm worried that it will make the program really slow. In particular, it seems like I'd need to do that every time one of the objects I'm working with is instantiated. If instead I could just call the sub as it's passed on the cli rather than having to go in and populate a dispatch table every single time, it would be preferrable.

Yeah, when I put that in the eval, it still fails as a syntax error. I don't know maybe I've hit a roadblock.

EDIT: I see your post, so what you're doing is making the string the entire call, like $obj->sub(); . Ok, I see what I was doing wrong, I was just having the subroutine call itself as a string and calling that. I'll try it this way.

my $emp = Person->new(); #now say I have access to $callIt, which holds the value, setFirstName("greg"), but I don't know that, because it's just been passed in to me my $subCall = $emp.'->'.$callIt; $subCall; #doesn't work, it just, well does nothing

Re: [perlFun] Calling a subroutine whos name is stored as a scalar
[In reply to]

Can't Post

The more you say what you don't have access to and what you don't know what is being passed to you tells me that you don't have a legitimate reason to do this and that you're attempting to hack their app. I'm not going to be involved with this.