Clinton
Clinton

Reputation: 23135

Confused about perl IPC::Run

I'm trying to use code like this:

run \@cmd, \$in, \$out, \$err;

As discussed in IPC::Run.

Of course, this complains about undefined variables.

So then I try this:

my $in;
my $out;
my $err;

run \@cmd, \$in, \$out, \$err;

print $in "Hello World";

But then on the print line I get issues with an undefined reference.

Am I doing something totally wrong here? And if so, what do I need to modify?

Upvotes: 1

Views: 1617

Answers (1)

numeric illustration
numeric illustration

Reputation: 326

The sample code on the IPC::Run page is assuming that you already have those variables / descriptors declared and setup elsewhere hence why once you set them up, it stopped complaining about that.

Printing to $in when it isn't a valid file handle will kick that error. You want to either just leave the file handle off of the print statement, or open a filehandle to a file you want to write to, and pass that to print.

See the doc pages on open and print for more info on those functions:

http://perldoc.perl.org/functions/open.html

http://perldoc.perl.org/functions/print.html

Also, I highly recommend that you use strict and warnings in your perl scripts if you are not already as it will catch many errors for you.

As LeoNerd mentioned, if you are not setting up the array @cmd to contain an array of commands that you want run, nothing is actually going to be executed in that call to run.

If you are just getting started with Perl and using CPAN modules, I highly recommend that you also start using Data::Dumper (in core Perl, nothing to install to use it, just put use Dumper; up top with your other use statments) to print out your variables as a way of debugging your code to get visibility into what is going on.

Upvotes: 1

Related Questions