CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
calling/executing a sub which I have as a string

 



brunob
New User

Jun 25, 2007, 12:35 PM

Post #1 of 8 (1350 views)
calling/executing a sub which I have as a string Can't Post

Hello all,

My apologies when there is an easy answer to my question which I failed to find on the internet :)

Suppose I have defined a sub my_sub normally I would execute it as follows:

&my_sub("inputstring");
sub my_sub { print $_[0];}



However, no I have the name of the sub as a string:


$subname = "my_sub('inputstring')";


and I want to execute the sub... in the ideal world I would
like to do something like:

execute($subname);

but that doesn't seem to work.


any help is appeciated!!

thanks,
Bruno


KevinR
Veteran


Jun 25, 2007, 4:42 PM

Post #2 of 8 (1349 views)
Re: [brunob] calling/executing a sub which I have as a string [In reply to] Can't Post

look into "do" or "eval", although your question is not easy to understand. Unless you are trying to run input as code it makes no sense to have the code (or the name of the sub as you say) as the value of a scalar. Are you sure you're not trying to use a reference to a sub routine?


Code
$sub = \&foo; 
$sub->('input');

sub foo {
print shift;
}

-------------------------------------------------


brunob
New User

Jun 26, 2007, 1:05 AM

Post #3 of 8 (1344 views)
Re: [KevinR] calling/executing a sub which I have as a string [In reply to] Can't Post

Hi Kevin,

Thanks for your help.

The thing I am trying to achieve is run a CGI script where I
pass another script and a sub in that script as an input string
via the URL. So I call the script like this:

http:// .../cgi-bin/my_script?script=otherscript&sub=my_sub&param=test


inside my_script there is the code

Code
require otherscript;


and in "otherscript" there is the sub "my_sub" which I would
like to run:

Code
&my_sub('test');


However, all I have is a string "my_sub".


Because I need to set up "my_script" very generic (it will produce html) I do not want to hardcode "my_sub" in it.


Hopefully I made myself clear.

thanks again,
Bruno


KevinR
Veteran


Jun 26, 2007, 2:01 AM

Post #4 of 8 (1342 views)
Re: [brunob] calling/executing a sub which I have as a string [In reply to] Can't Post

I think I understand what you want to do. I'm not sure how good of an idea it is to want to do it though. But anyways. Obviously you are sending query string data to "my_script". Assuming you already have parsed the data out of the query string and stored the values in scalars, this example shows how to use the data to run a sub routine and use the input as the argument:


Code
my $sub = 'my_sub';  
my $input = 'test';
$sub->($input); # a reference to my_sub

sub my_sub {
print shift;
}

-------------------------------------------------


brunob
New User

Jun 26, 2007, 8:10 AM

Post #5 of 8 (1340 views)
Re: [KevinR] calling/executing a sub which I have as a string [In reply to] Can't Post

Hi Kevin,

Thanks, this seems to work just as I want it to work.

I agree this is not the most esthetic solution.. but it enables me
to create a very generic script.

thanks again,
Bruno


KevinR
Veteran


Jun 26, 2007, 9:02 AM

Post #6 of 8 (1339 views)
Re: [brunob] calling/executing a sub which I have as a string [In reply to] Can't Post

It also allows users to send input that your script will try and run as code. You have to be careful about such a situation. Your scenario is probably not too "dangerous", but it's something most programmers would not recommend you do.
-------------------------------------------------


brunob
New User

Jun 26, 2007, 10:25 AM

Post #7 of 8 (1337 views)
Re: [KevinR] calling/executing a sub which I have as a string [In reply to] Can't Post

You are right. I am aware of this situation as it is a possible security gap.

The way I "solved" this is put the name of the script in a file and
let the name of that file pass as parameter.

So I would have a file, let's say "test.txt" with contents:

<begin file>
scripttorun.pl;sub_to_execute;parameters
...other relevant stuff
<end file>

Now the url I would call is:

http://../cgi-bin/myscript.pl?file=test.txt

"scrip.pl" reads test.txt and executes &sub_to_execute(parameters);

so the user is not free to run any code (s)he likes by passing it as paremeter. (S)he would need to have edit "test.txt" to do so...

And... if the user has gained write access to text.txt.. well.. then there is
no point in messing around using script.pl as the access to the system is
gained already.

Still, I would not use this method to handle crdit card transactions :)
For a simple website where I want to give some users some query rights
it will do.

Bruno


madvan
New User

Aug 2, 2007, 10:39 AM

Post #8 of 8 (1305 views)
Re: [brunob] calling/executing a sub which I have as a string [In reply to] Can't Post

hi bruno, can you write me an e-mail on mad_van@abv.bg please, just wanna ask you something. Or chek your In Box (MASSAGES) here :)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives