Executing Perl from R - perlQuote/shQuote? -
i'm trying run perl r using system
: assigning string (provided in r) variable , echoing it. (the system
call executes in /bin/sh
)
echo <- function (string) { cmd <- paste(shquote(sys.which('perl')), '-e', shquote(sprintf("$str=%s; print $str", shquote(string)))) message(cmd) system(cmd) } # fine: # echo('hello world!') # echo("'") # echo('"') # echo('foo\nbar')
however, if try echo
backslash (or indeed string ending in backslash), error:
> echo('\\') '/usr/bin/perl' -e "\$str='\\'; print \$str" can't find string terminator "'" anywhere before eof @ -e line 1.
(note: backslash in front of $
fine protects /bin/sh
thinking $str
shell variable).
the error because perl interpreting last \'
embedded quote mark within $str
opposed escaped backslash. in fact, perl echo backslash need do
> echo('\\\\') '/usr/bin/perl' -e "\$str='\\\\'; print \$str" \ # <-- prints
that is, need escape backslashes perl (in addition me escaping them in r/bash).
how can ensure in echo
string user enters string gets printed? i.e. level of escaping needed on r level?
i.e. there sort of perlquote
function analagous shquote
? should manually escape backslashes in echo
function? there other characters need escape given?
don't generate code. that's hard. instead, pass argument argument:
echo <- function (string) { cmd <- paste(shquote(sys.which('perl')), '-e', shquote('my ($str) = @argv; print $str;'), shquote(string)) message(cmd) system(cmd) }
(you use environment variable.)
(i've never used or seen r code before, pardon syntax errors.)
Comments
Post a Comment