Autoload — specify actions to be executed automatically at the beginning of every page access
Specify actions (in form of Perl subroutines or ITL tags) that are to be invoked automatically, on every page access. This step is performed before any page parsing occurs, and before the action or page is even determined.
The directive can be set to the name of a subroutine (Sub
or
GlobalSub
), or to a string containing ITL tags.
The return value from the code run is discarded.
Example: Simple Autoload example
Put the following in interchange.cfg
:
GlobalSub <<EOR sub simple_gsub { open OUT, "> /tmp/out"; print OUT scalar localtime, "\n"; close OUT; } EOR
Put the following in catalog.cfg
:
Autoload simple_gsub
Now, at each page visit, the file /tmp/out
will
contain the access time. This example is pretty useless and does not
convey good programming practice (the file opening part), but it does
show a practical, stand-alone example.
Example: Redirect page accesses
Let's say that a new page visit is "triggered" as a result of users
submitting a HTML form. At that point, mv_nextpage
contains the name of the page to display next, of course.
The following would redirect all accesses from
directory public/
to
directory private/
:
Autoload [perl] $CGI->{mv_nextpage} =~ s:^public/:private/:; [/perl]
Example: Temporary change of configuration directives
As you might know, on each page access, all catalog configuration directives (global and catalog) are "re-instantiated", and valid for the current page. This particularly convenient feature allows us to change (modify, add or delete) configuration directives as we see fit on a per-page basis, without worrying about them being persistent, and consequently, even without the need to re-set them back to original values.
The following example (put in catalog.cfg
) displays a different flypage for
Opera web browsers:
Autoload <<EOA [perl] if ($Session->{browser} =~ /opera/i) { $Config->{Special}->{flypage} = 'opera_flypage'; } [/perl] EOA
Interchange 5.9.0:
Source: lib/Vend/Config.pm
Line 3802 (context shows lines 3802-3836)
sub parse_routine_array { my($item,$settings) = @_; return '' unless $settings; my $c; if(defined $C) { $c = $C->{$item}; } else { no strict 'refs'; $c = ${"Global::$item"}; } my @mac; if($settings =~ /^[-\s\w,]+$/) { @mac = grep /\S/, split /[\s,]+/, $settings; } else { push @mac, $settings; } if(ref($c) eq 'ARRAY') { push @$c, @mac; } elsif($c) { $c = [$c, @mac]; } else { $c = scalar(@mac) > 1 ? [ @mac ] : $mac[0]; } return $c; }