Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Gearman::Client Return Value

by bichonfrise74 (Vicar)
on Oct 07, 2009 at 23:49 UTC ( #799836=perlquestion: print w/replies, xml ) Need Help??
bichonfrise74 has asked for the wisdom of the Perl Monks concerning the following question:

I playing with Gearman::Client and below are my codes. Basically what I wanted to do is to send a string from the client and have the worker split them and return two strings.
#!/usr/bin/perl # worker.pl use strict; use Gearman::Worker; my $worker = Gearman::Worker->new(); $worker->job_servers( '127.0.0.1:4730' ); $worker->register_function( 'split_it', \&my_split_function ); $worker->work() while 1; sub my_split_function { my $string = $_[0]->arg; my @new_string = split( ',', $string ); return \@new_string; } ------- #!/usr/bin/perl # client.pl use strict; use Gearman::Client; my $client = Gearman::Client->new(); $client->job_servers('127.0.0.1:4730'); my $result = $client->do_task( 'split_it', 'abc,def' ); print "Split: @$result\n";
When I run the client.pl, it will give an error output on the worker.pl something like
Not a SCALAR reference at /usr/local/share/perl/5.8.8/Gearman/Worker.pm line 365.
Does this mean that it is expecting the worker.pl to return a scalar reference? Since I'm trying to return an array reference, then it is failing?

Replies are listed 'Best First'.
Re: Gearman::Client Return Value
by kennethk (Abbot) on Oct 08, 2009 at 00:04 UTC
    I'm not familiar with the module in question, but the line in question reads:

    my $rv = ref $ret ? $$ret : $ret;

    It checks if what was returned was a reference, and if so dereferences it for return assuming it was a scalar. That seems strange to me, but perhaps it makes sense in the broader context the module was intended for. You could perhaps try "out thinking" it by returning a ref to an array ref (return \\@new_string;), but that smacks of trouble to me. I would think since this seems to involve a serialization protocol, you'd just end up with a stringified reference in that case. What would seem more logical to me would be to plan on returning a delimited string in place of your array in the true case, and splitting in the client function.

Re: Gearman::Client Return Value
by runrig (Abbot) on Apr 05, 2010 at 21:27 UTC
    You can only pass strings to and from gearman workers and clients. If you want to pass array references, you'll need to serialize/unserialize them with something like Storable freeze()/thaw() (or with JSON or some other option).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://799836]
Approved by ww
help
Chatterbox?
and nobody stirs...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2018-06-21 18:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (118 votes). Check out past polls.

    Notices?