![]() |
| > Home > Documentation > Latest documentation > Interchange Perl usage > perl |
|
perlExecutes the Perl code, contained in the tag's body text, and returns the result. Summary
ExamplesTag expansion example
Perl example
or similarly with positional parameters:
DescriptionThis tag allows you to embed Perl code within an Interchange page. The code will run under the restrictions of Perl's
Safe Perl's "warnings" and "strict" pragmas are both turned off and, since it blocks Perl's "use" command,
Safe This tag expands to the value returned by the enclosed code and so printing to STDOUT or STDERR is useless.
Object references are available for most Interchange tags and functions, as well as direct references to Interchange session and configuration values. See Interchange Perl objects for more information. The [perl] tag will remember variable values inside the current page, unless scoped with "my", so you can do the equivalent of a memory store and recall for a loop. Having said that, you must ensure that unscoped variables are initialised to a known value before they are used, as their default value cannot be relied upon. Also see the [calc] and [calcn] tags. ParameterstablesThis should be a whitespace-separated list of database tables you want to make available within your Perl code. If you would like to use database values in your Perl code then the tag must pre-open the table(s) you will be using. Here is an example using the products table:
If you do not pre-open your tables then your code will fail, with a runtime
Safe Even if you properly specify the tables to pre-open, some database operations will still be restricted because
Safe
Safe::Hole Interchange database tables can always be accessed as long as they are pre-opened by using an item first.
Safe For example, this will work because the first call to [accessories] "opens" the (default) products table:
If you remove the first [accessories] tag, then the $Tag->accessories() call will fail with a
Safe The moral of this story is to ensure that you pass all necessary tables in the [perl] tag. failureIf your code contains a compile or runtime error, and fails to evaluate (i.e. eval($code) would set $@), the [perl] tag will return the value set for the failure attribute. The error will be logged as usual. For example:
will return "It Broke" because the $Tag->data() call will fail under the
Safe no_returnIf no_return=true ("1", "Yes" or "True") then the Perl code's return value will be suppressed. You can retrieve the return value from the session hash via [data session mv_perl_result] until it gets overwritten by another [perl] tag's result value. If no_return is true then the [perl] tag will still return any output explicitly written with the &HTML or $Document->write() functions. If no_return is not set then the $Document->write() buffer will not be returned (unless you use $Document->hot(1) or $Document->send(), in which case the contents of the write buffer will probably appear before anything else on the page). See the Interchange Perl objects page for more information. Here is an example:
This will put the same price on the page as our earlier example, but $Session->{mv_perl_result} will be set to either "too low" or "ok".
subsIf you have set the AllowGlobal configuration directive then passing this parameter with a "1", "Yes" or "True" value will enable you to call GlobalSub routines from within the enclosed Perl code.
argIf arg="subs" then this is exactly the same as the subs parameter. globalIf you have set the AllowGlobal configuration directive then setting the global paremeter to "1", "Yes" or "True" will turn off
Safe
Full "use strict" checking is turned on by default when in global mode. You can switch strict checking off by using "no strict" within your code. Note that any strict errors will go to the Interchange error logs, and the [perl] tag itself will fail silently within the page. fileThis prepends the contents of the specified file or FileDatabase entry to the enclosed Perl code (if any), and then executes as usual. For example:
The above would execute myscript.pl and expand to its return value. Absolute filenames (filenames starting with "/" or containing "../") are prohibited by the NoAbsolute configuration directive. If the filename is not absolute then Interchange will first look for a file in the current directory. If it fails to find the named file in the current directory then it will look in the list set with the TemplateDir configuration directive(s). If it still fails to find the file then it will look for an entry by that name in the FileDatabase table. number_errorsAdd line numbers to the source code displayed in the error.log file. This is amazingly useful if some of the Perl is being generated elsewhere, interpolated into the [perl] body and then executed. eval_labelThis will will replace the "(eval ###)" in the error message with the specified label string. This is useful for quickly tracking down bugs when you have more than one [perl] block in the page - especially if you are also using short_errors. short_errorsIf set to a true value, syntax errors and the like in [perl] tags will log just the error, not the whole source code of the block in question. This is handy when you have the code open in an editor anyway, and don't want the error itself to get scrolled away when running "tail -f error.log". trim_errorsIf set to a number, and the error produced includes a line number, only the specified number of lines before and after the broken line will be logged, instead of the whole block.
|
| Home | Legal nonsense | Privacy policy | Contact us |