Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl equivalent of PHP's escapeshellarg

Tags:

php

perl

To escape the string to be used as shell argument we use the function escapeshellarg() in PHP. Does Perl have an equivalent function ?

like image 616
Zacky112 Avatar asked Jul 09 '10 11:07

Zacky112


2 Answers

String::ShellQuote, but most of the time this is not needed. You simply can avoid invoking the shell by careful programming. For example, system takes a list of arguments instead of a string.

Best practice:

use IPC::System::Simple qw(systemx);
systemx($command, @arguments);

require IPC::System::Simple;
use autodie qw(:all);
system([@allowed_exit_values], $command, @arguments);
like image 118
daxim Avatar answered Oct 03 '22 19:10

daxim


Perl can match the following stated function:

adds single quotes around a string and quotes/escapes any existing single quotes

http://php.net/manual/en/function.escapeshellarg.php#function.escapeshellarg

like this:

sub php_escapeshellarg { 
    my $str = @_ ? shift : $_;
    $str =~ s/((?:^|[^\\])(?:\\\\)*)'/$1'\\''/g;
    return "'$str'";
}
like image 43
Axeman Avatar answered Oct 03 '22 17:10

Axeman