2008/05/17

The Labs.Com Admin Lab Perl Lab Inline Perl Pages IPP Cookbook
Last update 2000/10/20
The Labs - Design & Functionality For The Net

Inline Perl Pages Cookbook

  1. Basic Usage
  2. Dynamic PNG
  3. Using Includes
  4. Persistant Variables & Connections
  5. More Examples
IPP Cookbook
1. Basic Usage
Write a small HTML-page like with a small inline part with perl test.html:

  ... 
  #perl 
  if($in{name}) { 
     foreach my $i (keys %in) { 
        print "<dt>$i = \"$in{$i}\"\n"; 
     } 
  } 
  #/perl 
  ... 
  <form method=post> 
  Name: <input name=name> <input type=submit value="  Submit  "> 
  </form> 

Then the page can call itself (no need to set action with <form> tag) and provides the entered fields in the associative array %in.

IPP Cookbook
2. Dynamic PNG

InlinePerlPages also allows some fancy usage like a dynamic PNG. Create a text-file named test.png, make it executable (chmod +x)

 #perl 
 use GD; 
 print "Content-type: image/png\n\n"; 
  
 my $im = new GD::Image(256,128); 
 ... 
 print $img->png; 
 #/perl 

Note: The file must start with #perl as first line (otherwise it will be interpreted as header already). Also check the GD-lib.

In case you require to set further HTTP header infos, like Pragma or Caching recommendations, insert the proper HTTP header lines like:

 print "Content-type: image/png\n"; 
 print "Pragma: ...\n\n"; 

then the content-type you can define.

IPP Cookbook
3. Using Includes

The #include <> allows you to easily cascade reusable code, without writing it direct into the .html files. Therefore you can save a lot of coding when you check what code you can write as includeable file.

Example:

 #include <inc/navigation:test> 
 .... 
 .. 

The inc/navigation file:

 #perl 
 my $a = shift(@ARGV);   # $a = 'test' 
 print "...."; 
 ... 
 #/perl 

IPP Cookbook
4. Persistant Variables & Connections

Persistancy is one of the main features of mod_perl. InlinePerlPages provides %arg which can be used freely for storing data and connection infos.

 unless($arg{mysession}) { 
    $arg{mysession} = tie %db,'MyDB','somedbstuff',...; 
 }  
 # --- more of your code 

You can also use %arg to pass info from one inline-perl section to another:

 #perl 
 $arg{me} = 10; 
 ... 
 #/perl 
  
 Some <b<HTML code</b> ... 
 #perl 
 $arg{me} += 10;  # $arg{me} = 30; 
 ... 
 #/perl 

Depending on your httpd.conf:

 MaxRequestsPerChild n 

determines the life-time of %arg.

Please Note: if you use IPP under multiple <Virtual> the %arg are not localized by default, but all %arg are seen and known to all <Virtual>'s, therefore localize them like this:

 $arg{"$ENV{DOCUMENT_ROOT}:myvar"} = $myvar; 

as $ENV{DOCUMENT_ROOT} is unique (usually) for each <Virtual>.

In other words, if you use IPP on several <Virtual> they all have access to %arg; be aware of this. Maybe in a later version of IPP we will localize %arg truly so no other <Virtual> can access it.

IPP Cookbook
5. More Examples

later as time goes by ...

                                                                                                                                   

Inline Perl Pages

Last update 2000/10/20

All Rights Reserved - (C) 1997 - 2008 by The Labs.Com

Top of Page

The Labs.Com