Re: Newbie Question
by snowcrash (Friar) on Oct 25, 2000 at 14:00 UTC
|
#!/usr/bin/perl -w
use strict;
### execute script and save content in $output
my $output = `./2.pl argument.txt`;
my $filename = "./output.txt";
### write output to file $filename
open (OUTFILE, ">$filename") or die "Can't open $filename: $!";
print OUTFILE $output;
close OUTFILE;
The backticks operator: `STRING` executes STRING, waits until it exits and returns
the output of STRING as a single string. See man perlop
for more information.
yt,
snowcrash | [reply] [d/l] |
Re: Newbie Question
by Malkavian (Friar) on Oct 25, 2000 at 13:55 UTC
|
The '?' in the argument makes me wonderif you're attempting to code some CGI.
If so, I'd suggest you check out CGI.pm.
The executing of a program from within perl is happily done using the exec command (perldoc -f exec).
Creating a file for output is as simple as:
open (OUTFILE, ">/path/to/file.txt") or die "Can't open file for text
+output!\n";
print OUTFILE 'Some text you want to print to the file';
close OUTFILE;
In the open, note the direction of the chevron. If it points towards the filename, it writes to it. If it points the other direction, it reads from it. There are other open modes, but that's just something to get you up and going
Hope this helps some
Malk
*I lost my .sig. Do you have a spare?* | [reply] [d/l] |
|
While you are quite right that the exec function in perl can be
used to execute a command, it is seldom used by itself. Usually it
is preceded by the fork function call.
To execute a program and fetch it's output, I would rather suggest either
using the qx() construct or doing it by hand, open(F, "program|").
Autark.
| [reply] |
Re: Newbie Question
by davorg (Chancellor) on Oct 25, 2000 at 15:18 UTC
|
If the script is on the same server as the original script
then you can run it using system.
If, however, as your example implies you are running
a CGI script on another server, then you'll need to use
something like the LWP module (available on the CPAN).
--
<http://www.dave.org.uk>
"Perl makes the fun jobs fun
and the boring jobs bearable" - me
| [reply] |
Re: Newbie Question
by Anonymous Monk on Oct 25, 2000 at 15:40 UTC
|
Ok.
I think I need to clarify some issues here. :o)
The setup is like this:
All the files are on the same server and the same directory.
The file to be executed is 1.pl and it requires a querystring '?' with a filename. 1.pl reads the content and outputs a html-response (its a menu-system).
I want to execute the 1.pl file (with the querystring/argument) from another file, 2.pl and save the response to a textfile.
Sorry for my ignorance, I am a ASP-programmer and dont know too much about perl.
| [reply] |
Re: Newbie Question
by wardk (Deacon) on Oct 25, 2000 at 18:20 UTC
|
assuming 1.pl is a CGI script, called via a browser with
http://something/cgi-bin/1.pl?file=filename.ext
you can get to the filename being passed in 1.pl simply by doing
something like this:
use CGI;
my $query = new CGI;
my $filename = $query->param("file"); # $filename now contains filena
+me.ext
Now you have the filename in the variable $file
| [reply] [d/l] |
|
English is not my first language, so I am having some trouble explaining what I want to do. :o)
You are correct in your assumption that 1.pl is usually call via a browser using:
http://www.server.com/cgi-bin/1.pl?filename.txt
But it is not the filename.txt I want.
I want the result of the above url/execution written to a file, using a second script, 2.pl
| [reply] |
|
The feasibility of this depends totally on the nature of the CGI script you're wanting to call by hand. CGI scripts by their nature expect to be called from a web server, expect their arguments to provided per CGI spec, and will provide CGI output. The script.pl?argument construct is a web/CGI thing, but since you're trying to call your script via standard system() methods, CGI methods don't apply. Fortunately, the "GET" method you're using makes it easy:
system("./1.pl filename.txt"); # to stdout, or:
my $output = `./1.pl filename.txt` # in $output
A security note: If, instead of using "filename.txt", you use a user-provided variable, make SURE your script uses taint-checking (-T argument, see perlsec) and un-taint the variable smartly before attempting to use it, or someone can easily put in a filename of "/etc/passwd" or "some.file;rm -rf /etc/httpd|", for example.
Since the script is a CGI script, its output will contain CGI headers. If this is OK, great. Otherwise, you will need to strip them out.
Perhaps you should consider an ASP technique for running a CGI script "within" the context of a page (with SSI this is called a "virtual include").
Hope this helps. | [reply] [d/l] [select] |
|
My intent was to quickly show how to get the passed parameter easily using perl's CGI.pm module
(since you indicated your experience was with ASP). Wasn't attempting to address what would be
done with the data.
Seems that your 2.pl could use LWP to call the above file, storing the results in a variable/array.
or you could execute 1.pl from the command line using CGI.pm
perl 1.pl file=filename.txt > outfile
which could also be done via a 2.pl program, shell script or .bat file.
Hope these bits and pieces are helpful | [reply] [d/l] |