Suppose I'm running a backticks or system command through a script that I've run from the command line. Is there anyway for me to stop that command midstream and have the script continue?
The application in question is a very simple MP3 shuffle player. What I want to be able to do is go to the next song while the current one is playing. Read more for the code.
#!/usr/bin/perl
use Data::Dumper;
open(PLAYLIST,"</somedir/myplaylist.lst");
my @songs;
while (<PLAYLIST>) {
chomp();
push(@songs,$_);
}
my $count = scalar(@songs);
print "We found $count songs\n";
while (scalar(@songs) > 0) {
my $song_num = rand(scalar(@songs));
print "*** Playing $songs[$song_num]\n";
`mpg321 '$songs[$song_num]'`;
splice(@songs,$song_num,1);
}
If it makes any difference, this is Perl 5.8.5 on SuSE Linux 9.2.
UPDATE: After taking my first steps into the larger world of fork/exec, I have something that works to the best that I can see. Read on for the new version. I'll probably eventually update it to not use ^C to get the next song so it's easier to stop the music. Thanks to RazorbladeBidet for gently pushing me to learn fork!
#!/usr/bin/perl
my $song_num;
my @songs;
my $pid;
sub catch_next {
my $signame = shift;
print "Going to next song!\n";
my $count = kill(15,$pid);
}
sub reaper {
$waitedpid = wait;
$SIG{CHLD} = \&reaper;
}
open(PLAYLIST,"</somedir/myplaylist.lst");
while (<PLAYLIST>) {
chomp();
push(@songs,$_);
}
$SIG{INT} = \&catch_next;
$SIG{CHLD} = \&reaper;
my $count = scalar(@songs);
print "We found $count songs\n";
while (scalar(@songs) > 0) {
$song_num = rand(scalar(@songs));
my $song = $songs[$song_num];
splice(@songs,$song_num,1);
print "*** Playing $song\n";
unless ($pid = fork) {
exec("mpg321", "$song");
}
waitpid($pid,0);
}
UPDATE 2: Thanks to bluto for alerting me that I don't need a seperate SIGCHLD handler. The final code is below:
#!/usr/bin/perl
my $song_num;
my @songs;
my $pid;
sub catch_next {
my $signame = shift;
print "Going to next song!\n";
my $count = kill(15,$pid);
}
open(PLAYLIST,"</somedir/myplaylist.lst");
while (<PLAYLIST>) {
chomp();
push(@songs,$_);
}
$SIG{INT} = \&catch_next;
my $count = scalar(@songs);
print "We found $count songs\n";
while (scalar(@songs) > 0) {
$song_num = rand(scalar(@songs));
my $song = $songs[$song_num];
splice(@songs,$song_num,1);
print "*** Playing $song\n";
unless ($pid = fork) {
exec("mpg321", "$song");
}
waitpid($pid,0);
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.