s-news
[Top] [All Lists]

Re: running a perl script in splus

To: Tony Plate <tplate@blackmesacapital.com>
Subject: Re: running a perl script in splus
From: Prof Brian Ripley <ripley@stats.ox.ac.uk>
Date: Thu, 23 Dec 2004 17:42:26 +0000 (GMT)
Cc: Joe Cerniglia <cj5815@yahoo.com>, s-news@lists.biostat.wustl.edu
In-reply-to: <6.1.0.6.2.20041223095714.063d2038@mailhost.blackmesacapital.com>
References: <20041223140351.17391.qmail@web50208.mail.yahoo.com> <6.1.0.6.2.20041223095714.063d2038@mailhost.blackmesacapital.com>
Under Windows there is also win3().
Under Unix, there is unix().

What shell dos() uses is set by the ComSpec variable, AFAICS. Under NT-based versions of Windows it defaults to cmd.exe, and that understands "2>&1". Under 16-bit versions of dos() (Win 95/98/ME) it defaults to command.com, which does not, but some alternative shells do.


On Thu, 23 Dec 2004, Tony Plate wrote:

Yes, but the details depend on your OS.

Under windows, you can use the dos() function. You need to make sure that your have perl in your PATH variable. The S-PLUS dos function passes the command string to the windows command line evaluator.
Here's two very simple examples to get started:

dos("perl -e 'print 1'")
character(0)
dos("perl -v")
[1] ""
[2] "This is perl, v5.6.1 built for MSWin32-x86-multi-thread"
[3] "(with 1 registered patch, see perl -V for more detail)"
[4] ""
[5] "Copyright 1987-2001, Larry Wall"
[6] ""
[7] "Binary build 635 provided by ActiveState Corp. http://www.ActiveState.com";
[8] "Built 15:34:21 Feb  4 2003"
[9] ""
[10] ""
[11] "Perl may be copied only under the terms of either the Artistic License or the" [12] "GNU General Public License, which may be found in the Perl 5 source kit."
[13] ""
[14] "Complete documentation for Perl, including FAQ lists, should be found on" [15] "this system using `man perl' or `perldoc perl'. If you have access to the" [16] "Internet, point your browser at http://www.perl.com/, the Perl Home Page."
[17] ""
dos("perl -e \"print 1\"")
[1] "1"


Quoting strings in "-e" commnads so that they get passed properly to perl is challenging. (I'm not sure it can actually be done when there is any level of nesting, and it probably differs among versions of windows.) Whenever the windows command line processor cannot process the command successfuly, either because it can't find the command, or because it can't grok the quoting, the dos() function returns an empty length character vector. E.g., here's a failed attempt to print a string.

dos("perl -e \"print 'hello world'\"")
character(0)
dos("perlnot -e \"print 1\"")
character(0)

Some versions (I don't think all, but I'm not positive about that) of the windows command line processor understand "2>&1", which can provide more informative messages, e.g.,:

dos("perl -e \"print 'hello world'\" 2>&1")
[1] "Can't find string terminator \"'\" anywhere before EOF at -e line 1."
dos("perlnot -e \"print 1\" 2>&1")
[1] "'perlnot' is not recognized as an internal or external command,"
[2] "operable program or batch file."


The quoting issues of course that for most perl programs, it's probably better to put the program in a file and pass the name of that file to perl with the -f option. If you're really keen on inline programs, you can explore perl's quoting functions, e.g.:

dos("perl -e \"print q{hello world}\" 2>&1")
[1] "hello world"


If you're stuck at the point of not even being able to find the perl program, check what PATH is getting passed:

dos("PATH")
[1] "PATH=C:\\Program Files\\Insightful\\splus61\\/cmd;...

The documentation for the S-PLUS dos() function should of course be read some time during attempting this type of thing.

-- Tony Plate


At Thursday 07:03 AM 12/23/2004, Joe Cerniglia wrote:


Is it possible to run a perl script from splus?


Thanks in advance.

Joe



__________________________________
Do you Yahoo!?
The all-new My Yahoo! - Get yours free!
http://my.yahoo.com


--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu.  To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message:  unsubscribe s-news

--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu.  To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message:  unsubscribe s-news



--
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

<Prev in Thread] Current Thread [Next in Thread>