1
votes

i am kinda new to perl and to programming in general. right now i am trying to learn a bit more about how i can make two perl scripts interact, and about parent/child processes. for that purpose, i wrote two little perl scripts (a.pl and b.pl) to teach myself a little more about these things:

a.pl:

#!/usr/bin/env perl
use strict;
use warnings;
print "\npick a card, any card you want!\n>";
my $card = <STDIN>;
my @cmd = ('./b.pl');
push @cmd, $card;

system(@cmd);

print "sorry, i can't tell you the trick. magician's code...\n";

b.pl:

#!/usr/bin/env perl
use strict;
use warnings;

my $card = "@ARGV";

print "\nis this your card?\n\n$card\n(y/n)>";
chomp(my $answer = <STDIN>);

exit if $answer eq "y";

print "will i ever be a true magician?\n\n" if $answer eq "n";

there are a few things about this that i would like to ask for some advice. first of all: the reason i passed the reaction to "y" back to the first script is that i wanted to understand how parent/child processes work. if i understood the perldoc of "system" correctly, this function puts the parent process (in my case a.pl) on hold until the child process (b.pl) is finished. now that explains why a.pl is being completed when b.pl dies under the condition "if $answer eq "y" ". but what if i want the parent process to die and the child process to continue under a certain condition (for example "if $answer eq "n" ")? as it is right now, the program would print both statements (the one from a.pl and from b.pl) when the second STDIN is answere with "n". what would be a smart way to do that?

another thing i was wondering about is that when i wrote the scripts, i put lines 5-9 of a.pl like this:

my $card = <STDIN>;
system('./b.pl $card');

which didnt work, because no matter what i entered into STDIN, the system function did not pass any arguments and @ARGV in b.pl always returned 0. is this because you cannot use a variable as an argument of system, or is there something wrong with the syntax?

as i said before, i am trying to learn as much as i can about perl and how programming works, so if you guys have any tips or tweaks on how to make this better, i would be more than happy!

thanks for your help, i really appreciate it!

Prawn

2
What you are trying to do is inter process communication, in short ipc. There is a whole perldoc page dedicated to that topic, but this is comparable heavy stuff for a beginner. I'd start with readline/ `` and then you might want to have a look at open2. - Patrick J. S.

2 Answers

2
votes

Usually you don't want to use system() to spawn a second perl process...
There are many cases where different perl processes need to "talk" to each other...
For general information about IPC (Inter Process Communication) see here.
The (perhaps) most common way to exchange information between different perl processes is "sockets": IO::Socket.

1
votes

The other thing,

system('./b.pl $card');

That is because you are using the single quote, if you used double quotes it should have worked. Single quotes are used as a literal string. In double quotes the variables are replaced with their value.

system("./b.pl $card");

More info on quotes: http://www.perlmonks.org/?node=quotes+in+Perl

(Unfortunately I cannot completely answer your question)