I'm stuck trying to figure out why my usage of IPC::Open2 isn't working how I expect.
This works:
perl -e 'package MyPack; sub foo { use IPC::Open2; my ($in, $out); my $pid = open2($out, $in, q[program --unbuffered]); print $in q|some json|; my $result = <$out>; warn "got [$result]\n"; close($in); close($out); waitpid($pid, 0); } 1; package Main; MyPack->foo();'
It prints my expected result in square brackets after taking some time to calculate the result.
But if I have lib/MyPack.pm:
package MyPack; sub foo { use IPC::Open2; my ($in, $out); my $pid = open2($out, $in, q +[program --unbuffered]); print $in q|some json|; my $result = <$out>; + warn "got [$result]\n"; close($in); close($out); waitpid($pid, 0); } 1;
and do:
perl -Ilib -MMyPack -e 'MyPack->foo();'
It returns immediately with an empty result set (json empty square brackets inside my printed square brackets). foo() in both cases is copy/pasted. Why doesn't it work from inside a .pm?
Edit for more: Fully copy/pasting the entire contents of the perl 1 liner that works in to lib2/MyPack.pm, and then calling:
perl -Ilib2 -e 'use MyPack;'
also doesn't work. What can it be about having the code inside a .pm that could stop it working?
Edit for solution: my input json string had single quotes, which behaved differently on the command line and inside a .pl or .pm. Removing the quotes made them both behave the same.
In reply to Why is IPC::Open2 behaving differently in a .pm? by sendu
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |