Using the system command we can cause a Bourne Shell process to start and execute an arbitrary command for us.
system "date"; # causes date to run, output to stdout
$status = system("date"); # get exit status of command
$command = "who | wc -l";
system $command;
$status = system "date &"; # send to bg, keep going, status indicates
# whether launch went well
Perl may avoid using the shell and call the program directly in
some cases if it feels it can. Will always do if you call with
a list of args:
system "grep glong phonelist"; # calls shell which calls grep system "grep", "glong", "phonelist"; # calls grep directly
Can use backticks for command substitution-like form, just like sh.
$timestamp = "today is " . `date`;
foreach (`who`) { # print everyone logged in
@people = split;
print "logged in: $people[0]\n";
}
Can "open" a command to read from/write to.
#!/usr/bin/perl -w
open(FINGER, "finger |") or die "can't finger";
while ($line = <FINGER>) {
chomp($line);
print "found: $line\n"; # prints out each line in finger output
}
close(FINGER);
open(PRINT, "| lpr -Pprinter") or die "can't print";
print PRINT "this goes to printer", " along with this";
close(PRINT);
Can open arbitrary pipes full of stuff. Can use to prefilter input with some other command or postfilter data with something else.