in reply to Subroutine question
Note also that your open call is going to be insecure if any of your 3 arguments is provided by the user. You probably want to do something like this for better security, unless you know you can trust all three arguments. Running Perl with taint-checking (-T) enabled helps if you're dealing with potentially bad/mischievous data.sub file_processing { my ($script, $arg, $file) = @_; my @ret; open(FILE, "$script $arg $file |") or die "can't do it: $!"; while (<FILE>) { chomp; next if /^#|none|unkno/i; push(@ret, $_); } close(FILE); return @ret; }
This has the same effect, but instead of passing the command to /bin/sh (or whatever your shell is) to parse into a command and arguments (which might include things like semi-colons allowing an evil person to execute other programs), it uses exec to forcibly pass things as discrete, known arguments directly to the script.my $pid = open(FILE, "-|"); if (!$pid) { die "Couldn't fork: $!" unless defined $pid; exec($script, $arg, $file) or die "Couldn't exec: $!"; }
|
|---|