=head1 NAME Embperl - Building dynamic Websites with Perl =head1 SYNOPSIS For a list of available Embperl documentation please view L with perldoc(1) or man(1). If Embperl is not yet installed, have a look at perldoc TOC.pod in the distribution archive. =head1 DESCRIPTION Embperl is a framework for building websites with Perl. For the beginner it's any easy to setup and use way of embedding Perl code in HTML pages. It delivers several features that ease the task of creating a websites, including dynamic tables, formfield-processing, escaping/unescaping, session handling, caching and more. If your demands grows it gives you the power to make your Web site object-oriented and build it out of small reusable components. If you don't like the idea of mixing up all your layout and code Embperl supports separating it in different objects (e.g. createing an MVC application). Of course Embperl doesn't ties you to HTML, it allows components to be from different source formats (e.g. HTML, WML, XML, POD, ...) and if necessary transforms it (for example via XSLT) to other output formats. This is achieved by diving the output generation in small steps, where each is processed by a plugable provider. Advanced user can create their own syntax definitions (for example tag libraries) and extent Embperl by writing their own providers and much more B This document describes the syntax and several features of Embperl. Please read also L, which describes how to configure Embperl and how to access the different Embperl objects and their data. Also take a look at L to learn how to use Embperl page as objects. Additionally there are a few introductions documents should consider to read: L, L and L. =head1 SYNTAX Embperl understands two categories of commands. The first one are special Embperl commands, and the second category consists of some HTML tags which can trigger special processing. Embperl commands can span multiple lines and need not start or end at a line boundary. Before the special Embperl commands are processed, and for the VALUE attribute of the INPUT tag (see below), all HTML tags are removed and special HTML characters are translated to their ASCII values (e.g., `<' is translated to `<'). You can avoid this behavior by preceding the special character or HTML tag with a backslash. This is done in case your favorite (WYSIWYG) HTML editor inserts tags like line breaks or formatting into your Embperl commands where you don't want them. All Embperl commands start with a `[' and end with a `]'. To get a real `[' you must enter `[['. Embperl does not use SGML comments (i.e., or similar things) because some HTML editors can't create them, or it's much more complicated. Since every HTML editor takes (or B take) `[' and `]' as normal text, there should be no problem. =head2 [+ Perl code +] Replace the command with the result you get from evaluating the Perl code. The Perl code can be anything which can be used as an argument to a Perl eval statement. (See L<"(Safe-)Namespaces and opcode restrictions"> below for restrictions.) Examples: [+ $a +] Replaces the [+ $a +] with the content of the variable $a [+ $a+1 +] (Any expression can be used) [+ $x[$i] +] (Arrays, hashes, and more complex expressions work) C Whitespace is ignored. The output will be automatically HTML-escaped (e.g., `<' is translated to `<') depending on the value of the variables C<$escmode>. You do not have to worry about it. =head2 [- Perl code -] Executes the Perl code, but deletes the whole command from the HTML output. Examples: [- $a=1 -] Set the variable $a to one. No output will be generated. [- use SomeModule ; -] You can use other modules. NOTE the semicolon! [- $i=0; while ($i<5) {$i++} -] Even more complex statements or multiple statements are possible. C Statements like if, while, for, etc., must be contained in a single Embperl command. You cannot have the if in one command block and the terminating `}' or else in another. C To define subroutines, use L<"[! Perl Code !]"> (see below) instead of [- ... -] to avoid recompilation of the subroutine on every request. =head2 [! Perl Code !] Same as [- Perl Code -] with the exception that the code is only executed at the first request. This could be used to define subroutines, or do one-time initialization. =head2 [* Perl code *] (only version 1.2b2 or higher) B This is similar to [- Perl Code -]. The main difference is, while [- Perl Code -] always has its own scope, all [* Perl code *] blocks runs in the same scope. This allows you to define "local" variables with a scope of the whole page. Normally, you don't need to use local, because Embperl takes care of separate namespaces of different documents and cleanup after the request is finished, but in special cases it's necessary. For example, if you want to recursively call an Embperl document via Execute. There is a second reason to use the [* Perl code *] instead of the [- Perl Code -]. If you like to use perl's control structures. Perl's if, while, for etc. can B span multiple [- Perl Code -] blocks, but it can span multiple [* Perl Code *]. Example: [* foreach $i (1..10) { *] [- $a = $i + 5 -] loop count + 5 = [+ $a +]
[* } *] The following B work: [- foreach $i (1..10) { -] some text here
[- } -] The same can be done with Embperl L (see below) [$ foreach $i (1..10) $] [- $a = $i + 5 -] loop count + 5 = [+ $a +]
[$ endforeach $] B [* ... *] blocks _must_ always end with a B<;>,B<{> or B<}> B [* ... *] cannot apear inside a html tag that is interpreted by Embperl (unless you disable the interpretation of such tags like table, input etc.) B There are still benefits of using [- ... -] and metacommands: - much better debugging in the log file. - no restriction on where they can be used. You can use them anywhere; even inside html tags that are interpreted by Embperl. =head2 [# Some Text #] (Comments) (only version 1.2b2 or higher) This is a comment block. Everything between the [# and the #] will be removed from the output. B The [* ... *] blocks are interpreted before the comment block, so they are executed also inside a comment. B Everything (except [* ... *]) is really removed from the source, so you can also use the [# ... #] block to take a part out of your document. =head2 [= =] (Internationalisation) Defines a string which should be translated into a local language. See L for details. =head2 [$ Cmd Arg $] (Meta-Commands) Execute an Embperl metacommand. B can be one of the following. (B varies depending on ). =over 4 =item B, B, B, B Everything following the B metacommand until the B, B, or B is only output if the Perl expression given in B is true. B and B work similarly. Example: [$ if $ENV{REQUEST_METHOD} eq 'GET' $] Method was GET
[$ else $] Method other than GET used
[$ endif $] This will send one of the two sentences to the client, depending on the request method used to retrieve the document. =item B, B Executes a loop until the B given to B is false. Example: (see eg/x/loop.htm) [- $i = 0; @k = keys %ENV -] [$ while ($i < $#k) $] [+ $k[$i] +] = [+ $ENV{$k[$i]} +]
[- $i++ -] [$ endwhile $] This will send a list of all environment variables to the client. =item B, B Executes a loop until the B given to B is true. Example: [- $i = 0 -] [$ do $] [+ $i++ +]
[$ until $i > 10 $] =item B, B Executes a loop for each element of the second B, setting the first B accordingly. Example: [- @arr = (1, 3, 5) -] [$ foreach $v (@arr) $] [+ $v +]
[$ endforeach $] =item B Inside of looks same as Perl next statement. You could also use the following syntax, which allows you to add an additional condition (or any other Perl code): [* next if ($foo) *] =item B Inside of looks same as Perl last statement. You could also use the following syntax, which allows you to add an additional condition (or any other Perl code): [* last if ($foo) *] =item B Inside of looks same as Perl redo statement. You could also use the following syntax, which allows you to add an additional condition (or any other Perl code): [* redo if ($foo) *] =item B B consists of zero, one or two names of hashes (with or without the leading %) and an optional array as third parameter. The B metacommand will generate hidden fields for all data contained in the first hash but not in the second hash. The default used for the first hash is C<%fdat>, C<%idat> is used for the second. If the third parameter is specified, the fields are written in the order they appear in this array. That is, all keys of the first hash must be properly sorted in this array. This is intended for situations where you want to pass data from one form to the next, for example, two forms which should be filled in one after the other. (Examples might be an input form and a second form to review and accept the input, or a Windows-style "wizard"). Here you can pass along data from previous forms in hidden fields. (See eg/x/neu.htm for an example.) If you use just the 'hidden' command without parameters, it simply generates hidden fields for all form fields submitted to this document which aren't already contained in another input field. Example:
[$ hidden $]
If you request this with http://host/doc.htm?field1=A&field2=B&field3=C the output will be
C This should only be used for a small amount of data, since the hidden fields are sent to the browser, which sends it back with the next request. If you have a large amount of data, store it in a file with a unique name and send only the filename in a hidden field. Be aware of the fact that the data can be changed by the browser if the user doesn't behave exactly as you expect. Users have a nasty habit of doing this all of the time. Your program should be able to handle such situations properly. =item B The var command declares one or more variables for use within this Embperl document and sets the B pragma. The variable names must be supplied as a space-separated list. Example: [$var $a %b @c $] This is the same as writing the following in normal Perl code: use strict ; use vars qw($a %b @c) ; NOTE 1: `use strict' within an Embperl document will only apply to the block in which it occurs. =item B (Only Embperl 1.2b5 and above) Defines a Embperl subroutine. Example: [$ sub foo $]

Here we do something

[$ endsub $] You can call this subroutine either as a normal Perl subroutine [- foo -] or via the Embperl::Execute function. [- Execute ('#foo') # short form -] [- Execute ({ sub => 'foo'}) # long form -] The difference is that the Execute function will reset the internal states of Embperl like they were before the subrountine call, when the subroutine returns. You may also pass Parameters to the subroutine: [$ sub foo $] [- $p = shift -]

Here we show the first parameter [+ $p +]

[$ endsub $] [- foo ('value') -] In Embperl 2.0.2 and up you can use a shortcut syntax for passing parameters: [$ sub foo ($p) $]

Here we show the first parameter [+ $p +]

[$ endsub $] This behaves the same as the example above, but in addition the parameters defined in this way are lexcialy scoped and therefore only available inside the subroutine. In addtion you can define some initial Perl code for the subroutine: [$ sub foo ($a, $b) my $c = $a + $b ; $]

The result is [+ $c +]

[$ endsub $] If you have a couple of commonly used subroutines you can define then in one file and import them into the modules where they are necessary: [- Execute ({ inputfile => 'mylib.htm', import => 1 }) -] This will import all subroutines from the file I into the current page where they could call just as a normal Perl subroutine. =item B Embperl 2.1.1 and above. C can be used to output the values of variables either to the output or to logfiles. The first parameter specifies the output channel: =over 4 =item out Output to the page output =item pre Same as C, but surrounded with a
 tag. This is the default, if
the output parameter is omitted.

=item log

Output to embperl log file

=item err

Output to stderr, which normally goes to the httpd error log.

=back

If the output parameter is omitted, C
 is assumed.

The following parameter can be a literal text (in quotes) or a list
of variables.

Example:

  [$ dump err 'dump test', %fdat $]

  [$ dump %fdat, $i, @array $]



=back

=head2 HTML Tags

Embperl recognizes the following HTML tags in a special way.  All others are
simply passed through, as long as they are not part of a Embperl
command.

=over 4

=item B, B
, B, B Embperl can generate dynamic tables (one- or two-dimensional). You only need to specify one row or column. Embperl generates as many rows or columns as necessary. This is done by using the magic variables $row, $col, and $cnt. If you don't use $row/$col/$cnt within a table, Embperl does nothing and simply passes the table through. Embperl checks if any of $row, $col, or $cnt is used. Embperl repeats all text between and
, as long as the expressions in which $row or $cnt occurs are defined. Embperl repeats all text between and , as long as the expressions in which $col or $cnt occurs are defined. See also L<"$tabmode"> (below) for end-of-table criteria. Examples: (see eg/x/table.htm for more examples) [- @k = keys %ENV -]
[+ $i=$row +] [+ $k[$row] +] [+ $ENV{$k[$i]} +]
This will show all entries in array @k (which contains the keys from %ENV), so the whole environment is displayed (as in the B example), with the first column containing the zero-based index, the second containing the content of the variable name, and the third the environment variable's value. This could be used to display the result of a database query if you have the result in an array. You may provide as many columns as you need. It is also possible to call a 'fetch' subroutine in each table row. =item B, B, B
    , B
      , B
      , B Lists and dropdowns or list boxes are treated exactly as one- dimensional tables. Only L<"$row">, L<"$maxrow">, L<"$col">, L<"$maxcol"> and L<"$tabmode"> are honored. $col and $maxcol are ignored. See eg/x/lists.htm for an example. =item B