The Perl language environment can interpret inline Perl scripts that you specify in a FUNCTION block of the Net.Data macro, or it can process external Perl scripts that are stored in separate files on the server.
Verify that the following configuration statement is in the Net.Data initialization file, on one line:
ENVIRONMENT (DTW_PERL) DTWPERL ( OUT RETURN_CODE )
See Environment Configuration Statements to learn more about the Net.Data initialization file and language environment ENVIRONMENT statements.
Japanese users: | Some characters in the Japanese SJIS character set can be misinterpreted by Perl as control characters. There is an open source package called jperl that solves this problem. Download and install the package, then include the statement use I18N::Japanese.pm in the header of the Perl script. |
Calls to external Perl scripts are identified in a FUNCTION block by an EXEC statement, using the following syntax:
%EXEC{ perl_script_name [optional parameters] %}Required: Ensure that perl_script_name, the Perl script name, is listed in a path specified for the EXEC_PATH configuration variable in the Net.Data initialization file.
%FUNCTION(DTW_PERL) perl1() { %EXEC{MyPerl.pl %} %}
There are two ways to pass information to a program that is invoked by the Perl (DTW_PERL language environment, directly and indirectly.
%DEFINE INPARM1 = "SWITCH1" %FUNCTION(DTW_PERL) sys1() { %EXEC{ MyPerl.pl $(INPARM1) "literal string" %} %}
The Net.Data variable INPARM1 is referenced and passed to the Perl script. The parameters are passed to the Perl script in the same way the parameters are passed to the Perl script when the Perl script is called from the command line. The parameters that are passed to the Perl script using this method are considered input type parameters (the parameters passed to the Perl script can be used and manipulated by the Perl script, but changes to the parameters are not reflected back to Net.Data).
Pass parameters directly on the call to the Perl script using one of the following methods:
name="value"
For multiple data items, separate each item with a new-line or blank character.
If a variable name has the same name as an output parameter and uses the above syntax, the new value replaces the current value. If a variable name does not correspond to an output parameter, Net.Data ignores it.
The following example shows how Net.Data passes variables from a macro.
%FUNCTION(DTW_PERL) today() RETURNS(result) { $date = `date`; chop $date; open(DTW, "> $ENV{DTWPIPE}") || die "Could not open: $!"; print DTW "result = \"$date\"\n"; %} %HTML(INPUT) { @today() %}
If the Perl script is in an external file called today.pl, the same function can be written as in the next example:
%FUNCTION(DTW_PERL) today() RETURNS(result) { %EXEC { today.pl %} %}
You can pass Net.Data tables to a Perl script called by the Perl language environment. The Perl script accesses the values of a Net.Data macro table parameter by their Net.Data name. The column headings and field values are contained in variables identified with the table name and column number. For example, in the table myTable, the column headings are myTable_N_j, and the field values are myTable_V_i_j, where i is the row number and j is the column number. The number of rows and columns for the table are myTable_ROWS and myTable_COLS.
REPORT and MESSAGE blocks are permitted as in any FUNCTION section. They are processed by Net.Data, not by the language environment. A Perl script can, however, write text to the standard output stream to be included as part of the Web page.
The following example shows how Net.Data generates a table by executing the external Perl script:
%define { c = %TABLE(20) rows = "5" columns = "5" %} %function(DTW_PERL) genTable(in rows, in columns, out table) { %exec{ perl.pl %} %message{ default: "genTable: Unexpected Error" %} %} %HTML(REPORT) { @genTable(rows, columns, c) return code is $(RETURN_CODE) %} The Perl script (perl.pl): open(D2W,"> $ENV{DTWPIPE}"); print "genTable begins ... "; $r = $ENV{ROWS}; $c = $ENV{COLUMNS}; print D2W "table_ROWS=\"$r\" "; print D2W "table_COLS=\"$c\" "; print "rows: $r "; print "columns: $c"; for ($j=1; $j<=$c; $j++) { print D2W "table_N_$j=\"COL$j\" "; } for ($i=1; $i<=$r; $i++) { for ($j=1; $j<=$c; $j++) { print D2W "table_V_$i","_","$j=\"Ý $i $j ¨\" "; } } close(D2W);
Results: genTable generates:
rows: 5 columns: 5 COL1 | COL2 | COL3 | COL4 | COL5 | -------------------------------------------------- [ 1 1 ] | [ 1 2 ] | [ 1 3 ] | [ 1 4 ] | [ 1 5 ] | -------------------------------------------------- [ 2 1 ] | [ 2 2 ] | [ 2 3 ] | [ 2 4 ] | [ 2 5 ] | -------------------------------------------------- [ 3 1 ] | [ 3 2 ] | [ 3 3 ] | [ 3 4 ] | [ 3 5 ] | -------------------------------------------------- [ 4 1 ] | [ 4 2 ] | [ 4 3 ] | [ 4 4 ] | [ 4 5 ] | -------------------------------------------------- [ 5 1 ] | [ 5 2 ] | [ 5 3 ] | [ 5 4 ] | [ 5 5 ] | -------------------------------------------------- return code is 0