LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-10-2011, 11:42 PM   #1
zak100
Member
 
Registered: Jul 2009
Posts: 262

Rep: Reputation: 2
PERL programming: sending arrays


Hi,

I want to send an array using PERL IO::Socket. I have written the following code:

Code:
use IO::Socket::INET;
$server=IO::Socket::INET->new(Local .......);
#i have skipped the whole command for avoiding typing
$server->recv(@arra, 2):
$avg=($array[0]+$array[1]);
print("server1 =", $avg);
$server->send("$avg"):
When I compile it says:
Code:
  usage:$sock->recv(BUF, LEN, [, FLAGS]) at ser1lab7.pl line 3

I have checked the internet it says i have to do serialization, but it doesnt provide any example.

Somebody plz guide me with this problem.

Zulfi.
 
Old 10-11-2011, 03:01 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Not sure I understand what you're asking; you discovered (via error message)
that send/recv only supports scalars. So you need to come up w/ a method to
convert your array into e.g. a parseable string before throwing it at
the respective method (and vice versa).



Cheers,
Tink
 
Old 10-11-2011, 03:02 AM   #3
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,591

Rep: Reputation: 145Reputation: 145
Serialization means in this case put the values of the array into a string and send the string. AFAIK there is no builtin function in perl to do serialization. So I think you have to do sth like

Quote:
my $str = '';

foreach (@arr) {
$str .= $_.",";
}
 
Old 10-11-2011, 03:10 AM   #4
pgpython
Member
 
Registered: Dec 2005
Location: Sheffield, UK
Distribution: Gentoo
Posts: 142

Rep: Reputation: 32
You cannot transmit data structures over sockets - only binary data. To get around this you need to serialize the structure, send it then unserialize it at the other end. Rather than try and come up with your method I recommend using a module from CPAN. Storable works well. Also recommend you can only serialize which can be reproduced at the other end. So for example an database connection cannot be serialized for this reason
 
Old 10-11-2011, 12:07 PM   #5
zak100
Member
 
Registered: Jul 2009
Posts: 262

Original Poster
Rep: Reputation: 2
Thanks. I would use the serialize option. Thanks for elaborating it.

Zulfi.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
perl - arrays hardbop200 Programming 8 10-28-2008 11:20 AM
Perl substitution with arrays cramer Programming 2 08-14-2006 11:07 PM
perl: concatenate arrays jrtayloriv Programming 2 01-23-2005 07:13 AM
PERL : Arrays confusing? fredgt Programming 3 12-19-2004 08:24 AM
arrays in perl BBQ_Matt Programming 3 09-23-2003 07:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration