Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Subroutine Return?

by Anonymous Monk
on Dec 19, 2003 at 22:37 UTC ( [id://315940]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Greetings esteemed Monks! I am writing a small bit of SOAP client code using SOAP::Lite though this is not necessarily a SOAP question. I wrote a small subroutine to make life easier for the .NET eccentricities when using SOAP::Lite. When I uncomment the commented lines below and comment the next two it works fine. However when I run it as below it fails. I can't see the difference. Is it something with references?
use SOAP::Lite; use strict; my $uri = "test_uri; my $proxy = "test_proxy"; my $service = SOAP::Lite ->uri($uri) ->proxy($proxy) ->on_action(sub{sprintf '%s/%s', @_ }); my $uname = "testuser"; my $pwd = "testpwd"; #my $username = SOAP::Data->name(username => $uname)->type('string')-> +uri($uri); #my $password = SOAP::Data->name(password => $pwd)->type('string')->ur +i($uri); my $username = dotnet_stuff("username", $uname); my $password = dotnet_stuff("password", $pwd); my $test = $service->Login($username, $password); print $test->result(); if($test->fault()) { my $error = join (', ', $test->faultcode, $test->faultstring); print "$error"; } sub dotnet_stuff { my $name = pop(@_); my $value = pop(@_); my $soap = SOAP::Data->name($name => $value)->type('string')->uri($ +uri); return $soap; }
Any help would be appreciated very much!

Replies are listed 'Best First'.
Re: Subroutine Return?
by jeffa (Bishop) on Dec 19, 2003 at 22:49 UTC
    I don't see the immediate error, but the way in which you retrieve subroutine arguments is probably not doing what you think it should be. You are passing the name first, but it is being stored in $value instead. The convention is to use shift, which pulls the first element, and not pop which pulls out the last element. Run this and notice the output:
    foo(1,2); bar(1,2); sub foo { print pop; print pop } sub bar { print shift; print shift }
    Also, you are not passing $uri as a parameter. I would rewrite (and rename) your subroutine to something like:
    sub get_data { my ($name,$value,$uri) = @_; return SOAP::Data->name($name => $value)->type('string')->uri($uri) +; }
    Hope this helps. :)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      D'oh...Thanks, forgot about the nature of pop vs. shift. Of course the login is failing because it is passing the name of the parameter rather than the value. Thanks again!
Re: Subroutine Return?
by tachyon (Chancellor) on Dec 20, 2003 at 04:23 UTC

    As an alternative to shift you can do this which lets you easily see all the args that the sub takes in what order....

    sub dotnet_stuff { my ($name, $value) = @_; my $soap = SOAP::Data->name($name => $value)->type('string')->uri($ +uri); return $soap; } # trap for unwary using @_ and forgetting list vs scalar context sub foo { my $arg = @_; # WRONG, scalar context sets $arg = NUM_ARGS in @ +_ 1 my ($arg) = @_; # CORRECT list context DWIM

    cheers

    tachyon

Re: Subroutine Return?
by eclark (Scribe) on Dec 20, 2003 at 00:06 UTC

    I believe you should be using shift instead of pop in dotnet_stuff().

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-19 07:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found