0
votes

I have created basic lua script to except a argument and print its value, using redis-cli its executing with no error and o/p is printed in log file.

$ cat test.lua
 redis.replicate_commands()
 local var = ARGV[1]
 print ( 'var ', var)
$ redis-cli --eval test.lua , A
 nil 
$

I want to run lua script using perl script :

$ cat eval.pl
#!/usr/bin/perl

use strict;
use warnings;
use Redis;

my $r = Redis->new(
    server    => '127.0.0.1:6379',
    reconnect => 3,
    every     => 1.5 * 1000000,
    conservative_reconnect => 1,
    no_auto_connect_on_new => 1,
    cnx_timeout            => 15,
);
eval {
    $r->connect;
};
if ($@) {
    print '$@ : '.$@;
}
my $result = $r->eval( "test.lua", 0, 'A' );
print "result : $result\n";
$

But i am getting error:

[eval] ERR Error compiling script (new function): user_script:2: '=' expected near 'end' , at /../lib/site_perl/5.8.7/Redis.pm line 163

my redis version is 1.991

error message is not descriptive enough to debug

Redis.pm's code at line 163 is

 141 sub __with_reconnect {
 142   my ($self, $cb) = @_;
 143 
 144   ## Fast path, no reconnect
 145   $self->{reconnect}
 146     or return $cb->();
 147 
 148   return &try(
 149     $cb,
 150     catch {
 151       ref($_) eq 'Redis::X::Reconnect'
 152         or die $_;
 153 
 154       $self->{__inside_transaction} || $self->{__inside_watch}
 155         and croak("reconnect disabled inside transaction or watch");
 156 
 157       scalar @{$self->{queue} || []} && $self->{conservative_reconnect}
 158         and croak("reconnect disabled while responses are pending and conservative reconnect mode enabled");
 159 
 160       $self->connect;
 161       $cb->();
 162     }
 163   );
 164 }

Am i using sub eval wrong way ?

1
Perl 5.8.7 is from twelve years ago. Why do that to yourself?Dave Cross
@DaveCross, :( yes i know that , but i working on a legacy productBharat
With Lua and Redis? Interesting legacy.simbabque

1 Answers

6
votes

Checking the Redis documentation linked from Redis, it seems the eval method takes the script contents as the first parameter, not the filename.

Try

open my $SCRIPT, '<', 'test.lua' or die $!;
my $result = $r->eval( do { local $/; <$SCRIPT> }, 0, 'A' );