Re: Send/receive array through IO::Socket?
by isotope (Deacon) on Dec 01, 2005 at 20:38 UTC
|
Your big picture problem is that you need to serialize your data before passing it along -- sockets know only a series of bits, not data structures. As suggested before, YAML is an excellent choice for most uses. Perhaps the more traditional method is Storable, but there are plenty of choices available.
| [reply] |
|
|
| [reply] [d/l] |
|
|
Not quite. Storable suffers completely different inconveniences from YAML. Storable can get tripped up if the levels of Storable on client and server aren't compatable. You have to be a bit careful if the endianness of the client and server are different (I think there's an "nstore" and "nthaw" for network-order storing) - assuming the levels of Storable are otherwise compatable.
But it is part of the core, so it's already installed. That inconvenience is unique to YAML. (Well, unique in the context of YAML vs Storable.)
| [reply] |
|
|
Storable has been a standard module for quite a while.
Phil
| [reply] |
|
|
Re: Send/receive array through IO::Socket?
by philcrow (Priest) on Dec 01, 2005 at 20:31 UTC
|
Sounds like you need YAML.
Phil | [reply] |
|
|
I forgot to mention that this is on a server I don't own, and getting modules installed is possible, but very difficult... So much so that I'd rather remain ugly than go through the process of getting another module. :-)
| [reply] [d/l] |
|
|
YAML is Pure Perl, so you don't need any special permissions or tools to install it. But if you still want an alternative, I once wrote a simple but efficient serializer/deserializer. You'll need to add something to know when you've reached the end of the structure.
| [reply] |
|
|
Re: Send/receive array through IO::Socket?
by jeffa (Bishop) on Dec 01, 2005 at 21:16 UTC
|
If you know, 100%, that the % sign will never appear in your data, then what you already have should be enough, shouldn't it? But yes, there are better ways to serialize your data, and YAML and Storable are two ways that work. A naive and straight-forward solution would be to use Data::Dumper and eval:
sub encode {
return Dumper [@_];
}
sub decode {
my $VAR1;
my $string = shift;
return eval "$string";
}
I have not fully tested this method out, and though it does work for most complex data structures that involve arrays, hashes, and references to those, i coded this to only handle arrays.
I recommend using more robust ways of serializing your data, such as Storable, YAML, or even XML. But i did feel inclined to post this code, at the very least to see if anyone can see ways to break this code. :)
| [reply] [d/l] |
|
|
decode("system('rm -rf /home/jeffa')");
Be careful with using eval() unless you're absolutely sure the string you're evalling is trustworthy. For data recieved from a network socket that level of trust is pretty hard to come by!
-sam
| [reply] [d/l] |
Re: Send/receive array through IO::Socket?
by zentara (Cardinal) on Dec 02, 2005 at 10:50 UTC
|
| [reply] |
|
|
| [reply] [d/l] |
|
|
If you need some client-server examples for Easy::TCP, let me know.
I'm not really a human, but I play one on earth.
flash japh
| [reply] |