Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

typecast int to string

by suarezry (Initiate)
on Aug 12, 2011 at 15:46 UTC ( #920045=perlquestion: print w/ replies, xml ) Need Help??
suarezry has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to set some values to a remote app using xml rpc. It expects one value (cardnumber, example "12345") as type string. But perl is sending it as type int.

I'm using perl 5.10. How do I set this variable in perl as type string???

Comment on typecast int to string
Re: typecast int to string
by jethro (Monsignor) on Aug 12, 2011 at 16:20 UTC

    I suspect it is not perl that is sending the string but your script or the module you are using. In that case it would be very helpful to know this script and/or the modules your are using.

    Similar to the particle-wave duality in physics you could say that there is a string-number duality in perl. You can't test a variable whether it is a number or a string because it is both. Ok, you can use a regex and find out that there is nothing in a string that looks like a number, but the following just works (and would at most print a warning message if you turn on warnings):

    > perl -e 'print 1+"buss";' 1

    If the module you are using is doing some automatic testing with regexes, you might have luck with just adding a space to your string. Otherwise there must be something to specify the type

Re: typecast int to string
by VinsWorldcom (Priest) on Aug 12, 2011 at 17:07 UTC

    Showing us some code would help.

    I was bit by a similar situation doing XML RPC to a Java based server that was asking for a boolean. I used RPC::XML to make the XML RPC process easy and ultimately solve the problem.

    In your case, the method would be something like:

    RPC::XML::string->new(12345)
Re: typecast int to string
by Perlbotics (Abbot) on Aug 12, 2011 at 17:33 UTC

    If you're using XML::RPC, this should work:

    sub as_string { my $val = shift; return sub { { string => $val }; }; } ... $xmlrpc->call( 'gimmeallyourmoney', { cardnumber => as_string( 12345 ) + } );

    Which generates:

    <?xml version="1.0" encoding="UTF-8" ?> <methodCall> <methodName>gimmeallyourmoney</methodName> <params> <param> <value> <struct> <member> <name>cardnumber</name> <value> <string>12345</string> </value> </member> </struct> </value> </param> </params> </methodCall>

    Update: Added patch to XML::RPC-wishlist.

Re: typecast int to string
by zentara (Archbishop) on Aug 12, 2011 at 18:21 UTC
    I just saw this yesterday in chromatic's free book Modern Perl

    Its a free download, full of useful tips.

    my $numeric_x = 0 + $x; # forces numeric context my $stringy_x = '' . $x; # forces string context my $boolean_x = !!$x; # forces boolean context

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (10)
As of 2014-12-27 12:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls