Roald Euller's Home Page

Example of a perl script calling a SAS programming running under cgi-bin. You will have to modify this to reflect your own computing environment. The script parses arguments passed in from a form and passes them to a SAS program as a sysparm. The SAS program is invoked so it writes to stdout. The perl script reads the SAS output and formats the output into an html page for the user.


lines 32-56: Read the environment variables entered on the form and build the appropriate SAS command line.
lines 58-73: Open pipes to the SAS process, execute the SAS program and read the output from stdin.
lines 74-88: Format the SAS output as an html page. Note that the SAS program knows nothing about the intended use of its output -- it merely streams it to stdout.


1    #! /usr/bin/perl -s
2    
3    $debug = 0;
4    
5    if ($debug==1) { $log = "debug.log"; }
6    else           { $log = "/dev/null"; }
7    
8    # set up various things that SAS needs on command line
9    
10   $sas     = "/usr/sas/sas";
11   $config  = "/usr/sas/config.sas611";
12   $tmp     = "/tmp";
13   $work    = "/SAStmp";
14   $sasopts = "-stdio -sasuser $tmp -config $config -work $work";
15   $prog    = "/webserver/a/https/cgi-bin/test_program.sas";
16   
17   # set up environment
18   # most vars unassigned when executing cgi-bin
19   
20   $home        = "/webserver/a/httpd";
21   $sasroot     = "/usr/sas";
22   $terminfo    = "/usr/sas/terminfo";
23   $terminfoadd = "/usr/sas/terminfo";
24   $path = "/usr/bin:/usr/local/bin:.";
25   
26   $ENV{'HOME'}=$home;
27   $ENV{'SASROOT'}=$sasroot;
28   $ENV{'TERMINFO'}=$terminfo;
29   $ENV{'TERMINFOADD'}=$terminfoadd;
30   $ENV{'PATH'}=$path;
31   
32   #$err = system("/usr/sas/utilities/bin/cleanwork $work");
33   
34   # get and decode name value pairs from form
35   
36   if ($ENV{'REQUEST_METHOD'} eq "get") { $buffer = $ENV{'QUERY_STRING'}; }
37   else { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); }
38   
39   @pairs = split(/&/, $buffer);
40   
41   # sysparm will be sent to SAS
42   # it consists of and argument count plus vars to be passed to program
43   # sysparm is built by concatenating in loop below
44   
45   $cnt = $#pairs + 1;    # sas starts counting at 1
46   $sysparm = "$cnt";
47   
48   foreach $pair (@pairs)
49   {
50       ($name, $value) = split(/=/, $pair);
51       $value =~ tr/+/ /;
52       $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
53       $sysparm = $sysparm." ".$value;
54   }
55   
56   $doit = "$sas $sasopts -sysparm '$sysparm'";
57   
58   # open pipe to stdin and fork sas process
59   
60   open(SAS, "-|") ||
61      (open(STDERR, ">$log")  &&
62       open(STDIN, $prog)     &&
63       exec($doit)            ||
64       exit(1));
65   
66   # slurp in sas output
67   
68   @sasoutput = <SAS>;
69   close SAS;
70   if ($#sasoutput==0) {
71      @sasoutput = ("SAS job failed\n");
72   }
73   
74   # generate html
75   
76   print "content-type: text/html\n\n";
77   print "<html>";
78   print "<title>$prog</title>";
79   print "<body>";
80   print "<hr>";
81   print "<pre>";
82   
83   print join("", @sasoutput);
84   
85   print "</pre>";
86   print "<hr>";
87   print "</body>";
88   print "</html>";