Download

Download the bsfperl.jar from http://bsfperl.sourceforge.net and put it into the WEB-INF/lib directory of your webapp. If you want to rebuild your Cocoon you should copy it also to the lib/local directory of your Cocoon sources, otherwise the JAR will be lost after the build.

Add the "script" generator to your sitemap and register the BSFPerl engine with BSF

<map:generators>
  ...
  <map:generator name="script"
      src="org.apache.cocoon.generation.ScriptGenerator">
    <add-languages>
      <language name="perl" src="net.sourceforge.bsfperl.PerlEngineImpl">
        <extension>pl</extension>
      </language>
    </add-languages>
  </map:generator>
</map:generators>

Add a match which calls the script generator to your sitemap

<map:match pattern="hello.pl">
  <map:generate src="hello.pl" type="script">
    <map:parameter name="userName" value="Perl Lover"/>
  </map:generate>
  <map:serialize type="xml"/>
</map:match>

Perl script (hello.pl)

use strict;

# 
# a bean scripting framework object, 
# $bsf, is provided to us by bsfperl.
# 

#get the output String buffer
my $bsf_output = $bsf->lookupBean("output");

#get the Parameters object
my $bsf_parameters = $bsf->lookupBean("parameters");

#get the value of the parameter "key" defined in the sitemap
my $username = $bsf_parameters->getParameter("userName");

#generate xml
$bsf_output->append("<xml>" . "Hello, " . $username . "!" . "</xml>");

(tested with 2.1.8)

See Also

PerlScriptGenerator (last edited 2009-09-20 23:42:17 by localhost)