1

Consider the following 2 scripts :

main.pl

#!/usr/bin/env perl

require 'lib.pl';
testSub('Hello World');

lib.pl

sub testSub
{
    my ($input) = @_;
    print $input, "\n";
}
1;

I need to run main.pl script remotely through SSH using sudo while including lib.pl as a dependency.

I know how to run a single Perl script through SSH using sudo :

$ ssh user@host 'sudo perl' < /path/to/local/perl/script

But, in this case, the following error is immediately raised :

Can't locate libs.pl in @INC (@INC contains: /usr/lib/perl5/5.10.0/x86_64-linux-thread-multi /usr/lib/perl5/5.10.0 /usr/lib/perl5/site_perl/5.10.0/x86_64-linux-thread-multi /usr/lib/perl5/site_perl/5.10.0 /usr/lib/perl5/vendor_perl/5.10.0/x86_64-linux-thread-multi /usr/lib/perl5/vendor_perl/5.10.0 /usr/lib/perl5/vendor_perl .) at - line XX.

But how could I do the same while including another script as a dependency ?

Thanks all in advance.

iceman94
  • 129
  • 2
  • 10

1 Answers1

1

Perl can only look at the local file system for includes and can not call back through a ssh channel to get data. So you either need to transfer the libs.pl to the other side before executing the program or just include its content directly in your program.

If you don't like to do this maybe Object::Remote can help you. But don't ask me how to use it, I've only heard a great talk about it.

Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172
  • Thanks for the answer. I'll look into `object::Remote`. In the mean time, would this behavior be the same if I replaced the 'require' line by a proper Perl Module and used a Package definition ? – iceman94 Jul 06 '14 at 18:57
  • Yes, it would be the same. Perl has no idea that it magically should tunnel back through the ssh connection and include files from the system where the ssh was started. – Steffen Ullrich Jul 06 '14 at 19:03