By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,848 Members | 1,375 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,848 IT Pros & Developers. It's quick & easy.

From variable to function?

P: 1
Hello,

I am writing a small Perl program to atest the availbility of a webservice.

So the idea is to call a known method of a known webservice and check the result.

To make the program flexible and to allow me to test several webservices I pass te following params from the command line

SOAP-ping.pl [ -u url ] [ -p proxy ] [ -m method ]

eg: perl SOAP-Ping.pl -u http://www.soaplite.com/Demo -p http://services.soaplite.com/hibye.cgi -m hi()

I pick up the params using the use Getopt::Std;

and pass them to the SOAP::Lite object.

Passing the URL & proxy is not an issue but the method is causing me a headegde.

As I get the method as a variable (string) I can not pass it to SOAP::Lite as this requires it in a different form.
Expand|Select|Wrap|Line Numbers
  1. my $soap = SOAP::Lite
  2.            -> uri($url)                     
  3.            -> proxy($proxy);
  4.  
  5. # this ain't working           
  6. my $result = $soap->$method;
  7.  
  8. #The code that works is
  9. my $result = $soap->hi();
  10.  
How can I make this last line variable so I can pass the method via the command line instead of 'hard coded' in de code?

Here is the full version of my test program
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. http://guide.soaplite.com/#writing%20a%20client
  3. http://www.robertprice.co.uk/robblog...ith_Perl.shtml
  4. #  Usage: SOAP-ping.pl [ -u url ] [ -p proxy ] [ -m method ]
  5.  
  6. ####################################
  7. # Modules                          #
  8. ####################################
  9. use Getopt::Std;
  10. use Time::HiRes qw (time);
  11.  
  12. use SOAP::Lite;
  13.  
  14. ####################################
  15. # Globals                          #
  16. ####################################
  17.  
  18. ####################################
  19. # Get the parameters from the user #
  20. ####################################
  21. %options=();
  22. getopts("u:p:m:",\%options);
  23.  
  24. my $url    = defined($options{u}) ? $options{u} : "http://localhost/";
  25. my $proxy  = defined($options{p}) ? $options{p} : "http://localhost/";
  26. my $method = defined($options{m}) ? $options{m} : "";
  27.  
  28. ####################################
  29. # Test Connection                  #
  30. ####################################
  31.  
  32. printf("Url: %s\nProxy %s\nMethod: %s\n",$url, $proxy, $method);
  33.  
  34. # Calculate the time it takes
  35. my $start = time();
  36.  
  37. my $soap = SOAP::Lite
  38.            -> uri($url)                     
  39.            -> proxy($proxy);
  40.  
  41. my $result = $soap->hi();
  42.  
  43. unless ($result->fault)
  44. {
  45.     print $result->result(),"\n";
  46. }
  47. else
  48. {
  49.     print join ', ', 
  50.       $result->faultcode, 
  51.       $result->faultstring;
  52. }
  53.  
  54. my $soaptime = time() - $start;
  55.  
  56. printf("%d\n%d\n",$soaptime * 1000, $soaptime * 1000);
  57.  
To test it:

perl SOAP-Ping.pl -u http://www.soaplite.com/Demo -p http://services.soaplite.com/hibye.cgi -m hi()

Thx in advance for anyone helping me out here

Regards

Marc
Jan 23 '08 #1
Share this Question
Share on Google+
2 Replies


KevinADC
Expert 2.5K+
P: 4,059
If will probably need to use a hash table that defines which method the variable passed in the command line should call:

Expand|Select|Wrap|Line Numbers
  1. my %coms = {
  2.     hi => \&hi;
  3.     foo => \&foo,
  4. }
  5.  
  6. my $result = $soap->$coms{$method}->();
so there is still some hard coding but you can pass in method names and the script will call the corresponding method. Or maybe someone knows of a different way to go about it.
Jan 23 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
Read the SOAP::Lite documentation, there might be an option in the new method that allows you to call method: call() I have never used SOAP::Lite so I have no experience with it's methods. My above suggextion should still wok if you wanted to go that route.
Jan 23 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.