Faq(3pm)
โจ NAME โจ
Embperl::Faq โ how to embed Perl code in your HTML docs ๐
(this is the Faq(3pm) page from the User Contributed Perl Documentation, bestie โ come get your answers โจ)
๐ CONTENTS ๐
Here's the whole vibe map, so you know what's where ๐บ๏ธ
- "Downloading, Compiling & Installing"
- "Common Problems"
- "Common Questions"
- "Escaping & Unescaping"
- "Debugging"
- "Customizing"
- "Optimizing & Fine Tuning"
- "Additional Help"
๐พ Downloading, Compiling & Installing ๐พ
okay so for the basics on downloading, compiling, and installing, go read "INSTALLATION" in INSTALL in the Embperl documentation. And listen โ please, PLEASE load Embperl at server startup. If you don't, various problems may result and you will be sad ๐ญ. The one exception to that rule: if you compiled mod_perl with USE_DSO, then you must not load Embperl at server startup โ not via a use in your startup.pl file, and not via PerlModule from your httpd.conf. Not even a little. ๐ซ
๐ง Is there a binary distribution of Embperl for Unix?
No. ๐ซถ
๐ช Is there a binary distribution of Embperl for Win32?
Win NT/95/98 binarys for Apache/perl/mod_perl/Embperl are available from ftp://theoryx5.uwinnipeg.ca/pub/other/ . A european mirror is at http://www.robert.cz/misc/ . ๐
๐จ I want to run Embperl with mod_perl under Apache. In what order should I do the compiling?
First mod_perl and Apache, then Embperl. That's the order, periodt. ๐
๐ต I'm getting:
../apache_1.3.0/src/include/conf.h:916: regex.h: No such file or directory
Try compiling Embperl again, like this:
make DEFS=-DUSE_HSREGEX
๐ I'm trying to build HTML::Embperl, and while running 'make' i get:
cc: Internal compiler error: program cc1 got fatal signal 11 make: *** [epmain.o] Error 1
GCC croaking with signal 11 frequently indicates hardware problems. See http://www.bitwizard.nl/sig11/ ๐ง
๐ค I have a lot of errors in 'make test' from mod_perl when using Embperl
Try recompiling Perl and all modules โ this can sometimes make those annoying error messages disappear! fr fr โจ
๐งช How can I prevent 'make test' from running some of the tests?
Like, say you don't allow CGI scripts, so 'make test' fails at CGI. How do you run just the other tests? Try:
$ make test TESTARGS="--help"
# and for just offline and mod_perl:
$ make test TESTARGS="-hoe"
๐ Running 'make test' fails with an error message at loading of Embperl (even though mod_perl compiled and tested cleanly!)
see "I get symbol ap_* undefined/cannot resolve ap_*" ๐
๐ I get symbol ap_* undefined/cannot resolve ap_*
This can happen when symbols in the Apache binary can not be found or are not being resolved correctly.
Some OS do this (for instance bsdos), and it can also happen if your Apache binary is set to strip symbol information out from binaries.
Try:
1.) make clean
2.) perl Makefile.PL
NOTE: answer _no_ to mod_perl support. (This is important!)
3.) make test
If that works, it means that your installation of Embperl is OK, but is having problems resolving symbols with Apache.
Try rebuilding Apache and mod_perl from scratch, and make sure you do not strip symbols out of either.
On some systems/linker you need to tell the linker explicitly to export such symbols. For example FreeBSD linker needs the "-export-dynamic" option.
If you don't succeed with this approach, try statically linking Embperl to Apache/mod_perl (please see the next question for step-by-step instructions on how to do this). ๐ซก
๐๏ธ How can I build a statically-linked copy of Embperl with mod_perl support?
- go to your mod_perl directory, change to
src/modules/perland edit the Makefile so that it contains the line
#STATIC_EXTS = Apache Apache::Constants HTML::Embperl
- add a definition for
EPDIRand change theONJ=line so that it looks like this:
EPDIR=/usr/msrc/embperl OBJS=$(PERLSRC:.c=.o) $(EPDIR)/Embperl.o
$(EPDIR)/epmain.o $(EPDIR)/epio.o (EP DIR)/epeval.o $(EPDIR)/epcmd.o
$(EPDIR)/epchar.o $(EPDIR)/eputil.o
- go to the mod_perl directory and run
perl Makefile.PL
- go to the Embperl directory and do
make clean
perl Makefule.PL
make
(to compile in mod_perl support)
- go back to the mod_perl directory and remake Apache by typing
make
Now you have successfully built a httpd with statically-linked Embperl. ๐
NOTE: If you want to stop here, you can skip to step 11. and run a 'make install' in the Embperl directory to finish.
But if you want to run Embperl tests and/or if you want to be able to use Embperl in offline or "vanilla" CGI mode, we need to continue:
- go back to the Embperl directory
- backup the file
test/conf/config.pl - now build Embperl again but _without_ mod_perl support
make clean
perl Makefile.PL
make
- restore your saved
config.pltotest/conf/config.pl(without this step, only the offline mode would be tested) - run
'make test'for Embperl - do
'make install'for Embperl
NOTE: You should do it in this order, or it may not work. โ ๏ธ
NOTE: It seems to be necessary to load Embperl at server startup, either byPerlModuleor in aPerlScript. See next question on how to do this.
๐ How do I load Embperl at server startup?
You can load Embperl at server startup by PerlModule or in a startup.pl:
- edit your
srm.conffile to read:
PerlModule HTML::Embperl
- edit your
startup.plfile to read:
use HTML::Embperl
NOTE 1: Either of these approaches can often 'fix' SIGSEVs in any mod_perl handler, not just Embperl. ๐ฉน
NOTE 2: When mod_perl is compiled as loadable module (i.e. with USE_DSO) you must not load Embperl at server startup time! ๐จ
๐ make test fails with a SIGxxxx, how can I obtain a stack backtrace from gdb?
The eaiest way is
make install -> if Embperl is installed, it's easier
gdb perl -> start the debugger with perl binary
set args test.pl -> set the arguments for perl
r -> start the program
-> Here you should receive the signal
share -> makes sure all symbols are really loaded
bt -> show the backtrace
To get some more information it would be a good idea to compile Embperl with debugging information enabled. Therefore do ๐
๐ How do I build Embperl with debugging information
edit the Makefile
search for the line starting with 'CC = ' add the -g switch to the end of the line
search for the line starting with 'LDDFLAGS = ' add the -g switch to the end of the line
type make to build Embperl with debugging infomation
now start the gdb as described before. ๐ต๏ธ
๐ make test fails with SIGXFSZ
This may occur when the filesize limit for the account, either test is running as or the test httpd, is too small. Embperl make test generates a really large logfile! Yu must increase the filesize limit for that accounts. ๐ญ๐
๐ฆ Embperl on SCO Unix
From Red Plait ๐
My OS is SCO Unix 3.2v4.2, Apache 1.3.4, perl 5.004_4, mod_perl 1.18 and Embperl-1.1.1
I done following:
- I made HTML-Embperl-1.1.1 with no mod_perl support ( when I builded it with mod_perl 1.18 I can`t link it because it don`t finds ap_XXX functions. When I manually insert
src/main/libmain.afrom Apache 1.3.4 I got message "Symbol main is multiple defined in/src/main/libmain.a.and perlmain.o" ). Then I "make test" - all tests was O`k. After this I "make clean", "perl Makefile.pl" with mod_perl support and "make install" - I installed mod_perl and "perl Makefile.PL", then "make"
- because I have`nt dynamical loading ( very old and buggy OS ) I had to manually change
src/modules/perl/perlxsi.cto insert bootstraps function`s and it`s invocations and also/src/Makefileto manually insert libXXX.a libraries
In access.conf I insert code:
PerlModule HTML::Embperl
<Directory /my_dir>
SetHandler perl-script
PerlHandler HTML::Embperl::handler
</Directory>
๐ Embperl and mod_perl on AIX
You need at least mod_perl 1.22. For mod_perl 1.22 and higher Embperl should compile out of the box on AIX. If you run into problems with undefined symbols (like "ap_*") make sure you have the newest mod_perl version (as of this writing this is mod_perl 1.24_01). ๐
๐ Embperl does not write to the logfile, because of missing permissions of the user Apache runs as.
The apache server is started as root, then set the effective uid to user "www", who can then write to the embperl logfile (owned by root) file handle that is passed along. However, if this log file handle is later accidentally closed, then reopen, the www user would have problem writing to it?
The reopen is only done when the logfile name changes. As log as you don't change the name on the logfile, the logfile will stay open.
The problem (in this case) is, that Embperl init function ,(Init in epmain.c) calls OpenLog will an second argument of zero. Which will only save the filename. The log will actually opened on the first write to it (or at the start of the first request). At this time your Apache has already switch to user www. This is done to allow one to change the logfile name before a request, but after the init is already called (which is done when you or Apache "use" the module)
The current solutions is to write something to the log, before Apache changes it's user (i.e. in the startup.pl) ๐ซถ
๐ Is it possible to install EmbPerl into a private directory on my Unix/Linux Internet Service Provider account of which I have no root privilege?
Like any other Perl module it can. Read "perldoc ExtUtils::MakeMaker", to see which parameters are needed for Makefile.PL to change the installation directory. Additionally, you have to change the @INC path to contain your private directory and possibly paths to other object files.
Here are the brief details:
Requirements:
- At least Perl 5.004_04
- cc or gcc (your isp must give you access to the gcc compiler)
- URI
- MIME::Base64
- HTML::Parser
- HTML::HeadParser
- Digest::MD5
- libnet
- libwww
- File::Spec (I believe you may have to install this too if you are using Perl 5.004_04 as it may not be a standard module)
Direction:
- Get your copy of EmbPerl (HTML-Embperl-x.x.tar.gz)
% tar -xvzf HTML-Embperl-x.x.tar.gz% cd HTML-Embperl-x.x% perl Makefile.PL PREFIX=/to/your/private/dir% make% make test% make install
Replace /to/your/private/dir with the path to the directory you want the module to be placed in. Now preface your CGI scripts with something like this:
[Alternative 1]
#!/usr/bin/perl -wT
use CGI::Carp qw( fatalsToBrowser ); #recommend using this to report errors on die or warn to browser
use lib '/to/your/private/dir/lib'; #for FILE::Spec
use lib '/to/your/private/dir/'; #to find Embperl
use lib '/to/your/private/dir/i386-linux/auto/HTML/Embperl'; #to find Embperl compiled stuff
#if for some very weird reason the above 'use lib' pragma directive doesn't work, see Alternative 2
use HTML::Embperl;
#your code below ...
๐ซ [Alternative 2] ๐ซ
#!/usr/bin/perl -wT
use CGI::Carp qw( fatalsToBrowser ); #recommend using this to report errors on die or warn to browser
BEGIN {
unshift @INC, '/to/your/private/dir/lib'; #for FILE::Spec
unshift @INC, '/to/your/private/dir/'; #to find Embperl
unshift @INC, '/to/your/private/dir/i386-linux/auto/HTML/Embperl'; #to find Embperl compiled stuff
}
use HTML::Embperl;
#your code below ...
okay so when you run make test, you might get a bunch of extra lil warnings that are lowkey just noise ๐ซ โ and the fix is to edit the test.pl that ships with EmbPerl, changing this:
BEGIN
{
$fatal = 1 ;
into this:
BEGIN
{
unshift @INC, '/to/your/private/dir/lib';
$fatal = 1 ;
...
because (plot twist) test.pl might not be able to find FILE::Spec if you've got it installed in a private directory for Perl 5.004_04. classic.
Then do the same kind of glow-up for the important file embpcgi.pl that you do for all your CGI scripts โ like tweaking the @INC the way we just showed โ so perl can find, in particular, the EmbPerl shared obj files. ๐ง
And when you call your CGI scripts like this:
http://www.yourdomain.com/cgi-bin/embpcgi.pl/templateFiles/myNifty.epl
the script should just work. periodt. โจ
๐ Common Problems ๐
the most common problems of ALL involve Escaping and Unescaping. they're so common that an entire section on "Escaping & Unescaping" is devoted to them. iykyk.
when i use a module inside an Embperl page, it behaves weird when the source changes ๐ญ
nothing weird here bestie. everything is well defined. let's just try to understand how Perl, mod_perl and Embperl work together:
perldoc -f use tells us:
Imports some semantics into the current package from the named module,
generally by aliasing certain subroutine or variable names into your
package. It is exactly equivalent to
BEGIN { require Module; import Module LIST; }
except that Module must be a bareword.
so what matters for us here is that use executes a require, and that is always done before any other code gets executed. ๐
perldoc -f require says (among other things):
..., demands that a library file be included if it hasn't already
been included.
and
Note that the file will not be included twice under the same specified
name.
so now we know (or should know) that mod_perl starts the Perl interpreter once when Apache starts, and the Perl interpreter is only terminated when Apache is terminated. out of these two things it follows that a module loaded via use or require is only loaded once and will never be reloaded, no matter if the source changes or not. ๐คทโโ๏ธ
so far that's just standard Perl. things get a lil more complicated when running under mod_perl (only Unix), because Apache forks a set of child processes as necessary, and from the moment they're forked, they run on their own and don't know about each other. so if a module is loaded at server startup time (before the fork), it's loaded in all children (this can be used to save memory, because the code will actually only reside once in memory) โ but when the module is loaded inside the child and the source changes, it could happen that one child has loaded an earlier version and another child has loaded a later version of that module, depending on the time the module is actually loaded by the child. ๐ตโ๐ซ
that explains why sometimes it works and sometimes it doesn't โ simply because different children have loaded different versions of the same module, and when you reload your page you hit different children of Apache! iconic behavior tbh ๐
now there's one point that's special to Embperl to add. since Embperl compiles every page in a different namespace, a module that doesn't contain a package foo statement is compiled in the namespace of the page where it's first loaded. because Perl will not load the module a second time, every other page will not see subs and vars that are defined in the loaded module. this could simply be avoided by giving every module that should be loaded via use/require an explicit namespace via the package statement. ๐
so what can we do?
- if a module changes, simply restart Apache. that always works. ๐ฅ
- use
Apache::StatInc. this will do astaton every loaded module and compare the modification time. if the source has changed, the module is reloaded. this works most times (but not all modules can be cleanly reloaded), and as the number of loaded modules increases, your server will slow down, because of thestatit has to do for every module. ๐ฎโ๐จ - use
doinstead ofrequire.dowill execute your file every time it is used. this also adds overhead, but it may be acceptable for small files or in a debugging environment. (NOTE: be sure to check$@after ado, becausedoworks likeeval)
why doesn't the following line work? ๐
[+ $var . "<b>". $foo . "</b>". $bar +]
see what we mean? this is an Escaping & Unescaping problem for sure. you need to escape <b> as ' <b> ' and you probably also need to read the section on "Escaping & Unescaping"... ๐
i'm getting: "Glob not terminated at ..." ๐ญ
this might be a problem with "Escaping & Unescaping" as well.
my HTML is getting stripped out ๐ค
sounds like a problem with Escaping & Unescaping again!
unless, of course, you've already read the section on Escaping & Unescaping and it's still happening... like if you're using optRawInput and your HTML is still being stripped out...
I am using optRawInput, and my HTML is still being stripped out! ๐ฑ
aha! well that's different! never mind..
it can be easy to accidentally set optRawInput too late in your code...
try setting it in an extra Perl block ([- $optRawInput = 1 -]) earlier in the code, or in the server config, and see if that doesn't solve the problem... (optRawInput must be set before the block that uses it begins, as the block which uses it shouldn't be translated). ๐ซถ
help! i got a SIGSEGV! ack! ๐
if Embperl is not compiled at server startup, it can cause error messages, SEGfaults, core dumps, buffer overflow, etc โ especially if you're using another module inside an Embperl page. as far as anyone can tell, this seems to be a Perl/mod_perl problem โ but maybe not. if you have any ideas, let me know. ๐
to see the steps for loading Embperl at server startup, please see the section "Downloading, Compiling & Installing".
NOTE: when mod_perl is compiled with USE_DSO it behaves vice versa and you may get SIGSEGVs when Embperl is loaded at server startup time.
i am having troubles with using Embperl in combination with Apache::Include inside a Apache::Registry script ๐ฉ
this is a known problem, but it's a problem with mod_perl rather than with Embperl. it looks like mod_perl clears the request_rec after the first subrequest, so that it later doesn't know which subrequest was intended (unless it's explicitly specified). try using:
Apache::Include->virtual("test.epl", $r);
(instead of just Apache::Include->virtual("test.epl"); where $r is the apache request rec) ๐
i can't get PerlSendHeader to work under Embperl? ๐ค
you don't need PerlSendHeader when using Embperl โ Embperl always sends its own httpd header. ๐
but how do i customize the header that Embperl is sending? ๐
you'll find the answer to this and many other header issues in the "Common Questions" section.
i can't figure out how to split a 'while' statement across two [- -] segments ๐ญ
that isn't surprising, as you cannot split Perl statements across multiple [- -] blocks in Embperl :) you need to use a metacommand for that. the [$while$] metacommand comes to mind... :)
for a list of all possible metacommands, see the section on Meta-Commands in the Embperl documentation.
[$ while $st -> fetch $]
#some html or other Embperl blocks goes here
[$ endwhile $]
newer Embperl versions (1.2b3 and above) support the [* *] which can be used for such purposes.
[* while ($st -> fetch) { *]
#some html or other Embperl blocks goes here
[* } *]
while the latter can use all Perl control structures, the first seems to me more readable and is better debuggable, because Embperl controls the execution of the control structure โ so it can do a quite better job in debug logging. ๐
my HTML tags like '<' '>' and '"' are being translated to <, > !!! ๐ค
hey! not you again!? i thought we already sent you to the "Escaping & Unescaping" section of the FAQ?!?! ;)
Netscape asks to reload the document ๐
if you have something like this in your source, it may be the problem:
<META HTTP-EQUIV="content-type" CONTENT="text/html;charset=iso-8859-1">
Netscape seems to have a problem in such cases, because the http header is only content-type text/html, while the META HTTP-EQUIV has an additional charset specified. if you turn optEarlyHttpHeader off, Embperl will automatically set the http header to be the same as the META HTTP-EQUIV. โจ
i get "Stack underflow" ๐ต
the problem often occurs when you have a <table> tag in one file and a </table> tag in another file and you include them both in a main page (e.g. as header and footer). there are two workarounds for this problem:
- Set
optDisableTableScan
this will avoid that Embperl takes any action on tables. you can disable/enable this (also multiple times) inside the page with
[- $optDisableTableScan = 1 -]
if you put this at the top of your header/footer which you include with Execute, then the main page will still process dynamic tables. ๐
- Add a
<table>as comment
add the following to the top of the footer document:
<!-- <table><tr><td> -->
this will work also, because Embperl (1.x) will not scan for html comments ๐ฆ
๐ Common Questions ๐
okay so bestie, the questions that come up THE MOST are all about "Escaping & Unescaping" โ like, genuinely so many that the entire next section is devoted to them. but the less common ones? they live right here:
How can I get my HTML files to be converted into Perl code which, as a whole, could then be compiled as function so that I could, for instance, fetch Perl docs from the Formatter table and compile them the way AUTOLOAD does โจ
Short answer: Embperl cannot turn your HTML into one single piece of Perl-code. but what you can do is wrap the call to Execute inside a Perl function, and then let AUTOLOAD call it. that's the whole move ๐
I have an HTML page which is dynamically generated at runtime and should be post-processed by Embperl. How can I do this? ๐ฆ
- Generate the page inside a normal
CGI/Apache::Registryscript and drop the result into a scalar โ then you can callHTML::Embperl::Executeto post-process your document.Executecan either send the document straight to the browser, or tuck it into another scalar for further processing. your choice, main character. - Use
EMBPERL_INPUT_FUNC(1.1b1 and above). with this configuration directive you can specify a custom input function which reads the HTML source from the disk or even from a database. Embperl also gives you the functionProxyInput, which lets you pull input from an entirely different web server. iconic. - Go look at the module
Apache::EmbperlChain, which is able to chain multiple modules โ Embperl included โ together.
How can I customise the header that Embperl is sending? ๐
you can literally just write it as
<meta http-equiv="Content-Type" content="text/html">
(Embperl will automatically insert all meta http-equiv tags into the http header) โจ
or use %http_headers_out
[- $http_headers_out{'Content-Type'} = 'text/html' -]
or (only when running under mod_perl) you can use
[- $req_rec -> content_type ('text/html') -]
Can I use Embperl to send cookies? ๐
Yes. Embperl sends its own headers, so all you have to do to send cookies is remember to print an additional header. that's it, that's the task.
Example Code:
- in documents, add
<META HTTP-EQUIV="Set-Cookie" CONTENT="[+ $cookie +] = [+ $value +]">
- or use
%http_headers_out
[- $http_headers_out{'Set-Cookie'} = "$cookie=$value" -]
- or โ using mod_perl's functionality โ use
[- $req_rec -> header_out("Set-Cookie" => "$cookie=$value"); -]
NOTE: you make also take a look at Embperls (1.2b2 and above) ability to handle sessions for you inside the%udatand%mdathashes.
Can I do a Redirect with Embperl? ๐ฅ
the following way works with mod_perl and as cgi:
[- $http_headers_out{'Location'} = "http://www.ecos.de/embperl/" -]
and the status of the request will automatically be set to 301. no cap.
or use the mod_perl function Apache::header_out.
Example Code:
[-
use Apache;
use Apache::Constants qw(REDIRECT);
$req_rec->header_out("Location" => "http://$ENV{HTTP_HOST}/specials/");
$req_rec->status(REDIRECT);
-]
If there is nothing more to do on this page, you may call exit directly after setting the status. bye bestie ๐
Can I serve random GIFs with Embperl? (Will Lincoln Stein's GD.pm module work with Embperl??) ๐
as always, there is more than one way to do this โ especially because this is really a question of how you're coding your HTML more than how you're coding your Embperl.
Here are some ideas:
- You could include an
IMGtag which points to yourcgi-bin, where a regular CGI script serves the graphics. - You could be running
Apache::Registry, which can generate on-the-fly GIFs using GD. (This is just the same as if you were including the GD image from a static page or from another CGI script, but it allows all of the appropriate logic to live in a single document, which might be appropriate for some Embperl users).
If you think of another way, or come up with some sample code, I'd love to hear from you, so that I could add it to the FAQ... ๐ซถ
Can I use Embperl as a template for forms? Can I make form values persist (like with "vanilla" CGI)? Does Embperl rewrite my template file so that parameters of things like INPUT/TEXTAREA/SELECT persist? ๐
Yes, bestie. Your page design staff should just be able to say <input name="foo"> and let the default attributes of "foo" be defined elsewhere โ for instance in a settings file. In this case, %fdat should be pre-set with your default values. Setting $fdat{foo} = "abc" will cause Embperl to change the above code to <input name="foo" value="abc">. slay.
Does Embperl automatically add HIDDEN fields? ๐
The [$hidden$] metacommand creates hidden fields for every entry in %fdat which was not used by any other input tag so far.
You can also try something like this:
[-
$fdat{foo} = "abc" ;
$fdat{bar} = "xyz" ;
-]
<input name="foo">
[$hidden$]
and Embperl will create:
<input name="foo" value="abc">
<input type=hidden name="bar" value="xyz">
For a list of all possible metacommands, see the section on Meta-Commands in the Embperl documentation.
What about security? Is Embperl Secure? ๐
Just like anything else, Embperl is as secure as you make it. Embperl incorporates Safe.pm, which will make it impossible to accidentally access other Packages โ it also permits the Administrator to disable Perl opcodes, etc.
For more on security, please see "(Safe-)Namespaces and opcode restrictions" in Embperl in the Embperl documentation.
Is there any plan to make Embperl an Object so someone could subclass it and override certain of its methods? (For example, I'd like to let it parse the file for me, but then let me control the manipulation of the form tags.) ๐ง
Embperl is going to be an Object from version 1.2b1. This, among other things, make it re-entrant, so that you will be able to call Execute from within an Embperl page. It will also mean that Embperl will come with hooks, which will allow you to alter or change the way Embperl processes code. The details have not all been worked out yet, but I'm working on it... :)
Are Embperl routines currently pre-compiled or even cached, or are only fragments cached? โญ
All embedded Perl code is compiled the first time it is executed and cached for later use. The second time the code is executed, only the precompiled p-code is called.
Every code block is compiled as a single subroutine. The HTML text between the Perl block is still read from the file.
Why are Perl blocks broken up into single subroutines? ๐
- It makes it easier to process the HTML tags between the Perl blocks โ this gives you more control over what's happening
- If you compiled _everything_ to Perl, you would hold all of the HTML text in memory, and your Apache child processes would grow and grow... But often-accessed documents are still held in memory by your os disk cache, which is much more memory-efficient.
- There is only so far that you can go with precompiling until you reach the point of diminishing returns. My guess is that converting dynamic tables and other HTML processing to Perl at this point in Embperl's development would actually slow down operation.
Can I pass QUERY_STRING information to an HTML::Embperl::Execute call? ๐
With Embperl 1.0 and higher, you can do this. QUERY_STRING is set as $ENV{QUERY_STRING} by default. Alternatively, you can use the fdat parameter to pass values to %fdat.
How to include other files into Embperl pages? ๐ธ
"I am using embedded Perl on my site and am curious if I can use it for server side includes. I want to embed the contents of file x.html into file y.html such that whenever I change x.html, displaying y.html will also reflect this change. How do I do it using embedded perl?"
You need Embperl 1.2b4 or above. Then you can say inside of y.html:
[- Execute ('x.html') -]
EmbPerl iteration without indexing ๐ฆ
I have a rather large table in a database which I'd like to display using EmbPerl. All of the examples show a process of fetching all the data first, then iterating through it using $row and $col, like this:
[-
$sth = $dbh -> prepare ("select * from $comptbl order by SubSystem");
$sth -> execute;
$dref = $sth -> fetchall_arrayref;
-]
<TABLE>
... $dref -> [$row][0] ...
</TABLE>
I'd prefer to fetch the data one row at a time, how can I do this?
For solution 1 you may write
<table>
[$while $rref = $sth -> fetch $]
<tr>....</tr>
[$endwhile$]
</table>
Solution 2 should work like this
<table>
<tr> [- $dummy = $row ; $rref = $sth -> fetch -]
....
</tr>
</table>
The table ends when the expression where $row is used in some way returns <undef>. So also there is no releation between $row and the fetch, both conditions are met.
How to display arrays with undef values in it? ๐
I'm doing a search on a table where some of the columns have NULL and non-NULL values. DBIx::Recordset has no problem reading this values. The problem is that I then tried to print these values out in a table using Embperl's table feature, like this.
<TABLE>
<TR>
<TD>$set[$row]{column_name1}</TD>
<TD>$set[$row]{column_nameN}</TD>
</TR>
The problem is that I got 5 rows instead of the 15 that I was expected. I have been trying all kinds of tweaks to the arguments to the Search function and getting nowwhere, until I re-read the Embperl docs. Embperl will not print out a table row if one of the columns has an expression that is undefined. This is a problem since DBIx::Recordset (and DBI) natually uses undef to represent a NULL value for a column. So I made a slight modification to my embperl code.
<TABLE>
<TR>
<TD>defined($set[$row]{column_name1}) ? $set[$row]{column_name1} :
"UNDEF"</TD>
. . .
<TD>$set[$row]{column_nameN} ? $set[$row]{column_nameN} : "UNDEF"</TD>
</TR>
Now all 15 rows appear as expected, with "UNDEF" representing the NULL values in the database.
Another way top solve you problem may be:
<TABLE>
<TR>
[- $r = $set[$row] -]
<TD> [+ $r -> {column_name1} +] </TD>
. . .
<TD> [+ $r -> {column_nameN} +] </TD>
</TR>
This will only refer one time to $row and the expression is defined, as long as the row could be fetched from the db. All NULL fields will be displayed as empty table cells.
โจ Escaping & Unescaping โจ
๐ Escaping & Unescaping Input
okay bestie, listen โ by default Embperl removes all HTML tags from the Perl source. why?? because so many high-end WYSIWYG HTML editors (looking at you, MS Front Page ๐) love to insert HTML tags like <FONT> and <COLOR> in the most random places ever, like literally in the middle of your Perl code. rude honestly.
This Embperl feature is what keeps stuff like this:
[- $var = 1; <br>
$foo = 2 -]
totally permissible, so you can type Perl code while you're marking up pages in an editor, all at once. In this example, Embperl would remove the unnecessary <br> tag and therefore make Perl happy. And if Perl is happy, we are all happy. ๐
It is not difficult to change this behavior, if you're the kind of person who codes HTML in an ascii editor (like vi or emacs). respect. ๐ซถ
If you use a high-level HTML editor, you shouldn't have any problems with input escaping, because the editor will, for example, write a '<' as '<' in the HTML code. Embperl translates this back to '<' and therefore it knows that this wasn't an HTML tag which should be removed. slay. โจ
Problems with input escaping only occur if you use an ascii editor. Then you will need to escape input (see the next section for details on how to do this).
To see the exact steps taken by Embperl to process a Perl-laden document, please see the section Inside Embperl in the Embperl documentation.
๐ Ways To Escape Input:
- Escape it ->
\<H1>- NOTE: Inside double quotes you will need to use
\\(double backslash), since Perl will remove the first Escape itself. - Example: In most cases
'\<tr>'but inside double-quotes"\\<tr>"
- NOTE: Inside double quotes you will need to use
- Turn off Escaping for all input by setting the
optRawInputinEMBPERL_OPTIONS - Learn to avoid using HTML tags inside Perl code. Once you get the hang of it, you'll love it. fr fr. ๐
Here is one example of how to do it:
[- $output = "<bold>Hello world</bold>" -]
[+ $output +]
write
[- $output = "Hello world<bold>" -]
this outputs
Hello world
or
<bold>[+ $output +]</bold>
this outputs
<bold>Hello world</bold>
And here is another example of how to do it:
[-
@a = ('a', 'b', 'c') ;
foreach $i (0..2)
{
$output. = "<tr><td>Row $a[$i]</td></tr>" ;
}
-]
<table>
[+ $output +]
</table>
The output here would be:
<table>Row aRow bRow c</table>
The Embperl version is
[-
@a = ('a', 'b', 'c') ;
-]
<table>
<tr><td>Row [+ $a[$row] +]</td></tr>" ;
</table>
The output will be
<table>
<tr><td>Row a</td></tr>" ;
<tr><td>Row b</td></tr>" ;
<tr><td>Row c</td></tr>" ;
</table>
And another: This elegant solution shows you how to take advantage of Embperl's ability to create dynamic tables:
[-
use DBI;
my $dbh =
DBI->connect("DBI:mysql:database:localhost","Username","Password") ||
die($!);
$hstmt = $dbh->prepare("select ID, Heading from Shops order by Heading");
$hstmt->execute();
$dat = $hstmt->fetchall_arrayref() ;
$hstmt->finish();
$dbh->disconnect();
-]
<table border=1>
<tr><td>[+ $$dat[$row][$col] +]</td></tr>
</table>
This HTML code will then display the contents of the whole array. iconic behavior tbh. ๐
๐ธ Escaping & Unescaping Output
Embperl will also escape the output โ so <H1> will be translated to <H1> ๐ฆ
To see the exact steps taken by Embperl to process a Perl-laden document, please see Inside Embperl in the Embperl documentation.
๐ Ways To Escape Output:
- Escape it ->
\\<H1>- (You need a double backslash
\\, because the first one is removed by Perl and the second by Embperl.
- (You need a double backslash
- set
$escmode = 0->[- $escmode = 0 ; -] - set
SetEnv EMBPERL_ESCMODE 0in yoursrm.conf
๐ Debugging
๐ญ I am having a hard time debugging Embperl code
Have you, umm, checked the error log? ;) ๐ซ
Have you tried setting debug flags higher by resetting EMBPERL_DEBUG in the server config files? (And still higher? :)
dbgMem isn't usually very useful as it always outputs a lot of allocation. dbgFlushLog and dbgFlushOutput should be used if (and only if) you are debugging SIGSEGVs. ๐
For easy debugging, you can tell Embperl to display a link at the top of each page to your log file. Then every error displayed in an error page is a link to the corresponding position in the logfile, so you can easily find the place where something is going wrong. lowkey genius.
For more on using HTML links to the Embperl error log, see "EMBPERL_DEBUG" in Embperl in the Embperldocs.
๐ Embperl is running slow.
There are some debugging settings which may cause Embperl to drastically slow down. If you are done with debugging, set debugging bits back to normal.
Also, using dbgFlushLog and dbgFlushOutput will make execution much slower. These are only intended for debugging SIGSEGVs.
Never set all debugging bits! ๐จ not even once bestie.
๐ How can I improve Embperl's performance?
- Load Embperl at server startup. This will cause UNIX systems to only allocate memory once, and not for each child process. This reduces memory use, especially the need to swap additional memory.
- Disable all unneeded debugging flags. You should never set
dbgFlushLogdbgFlushOutput,dbgMemanddbgEvalNoCachein a production environment. ๐ซ - You may also want to take a look at the available options you can set via
EMBPERL_OPTIONS. For exampleoptDisableChdir, will speed up processing because it avoid the change directory before every request. โจ
๐จ Customizing
๐ง How can I fiddle with the default values? How can I override or alter this or that behavior?
Usually, defaults are set in a way that is likely to make most sense for a majority of users. As of version 1.0, Embperl allows much more flexibility in tweaking your own default values than before. Take a look at EMBERPL_OPTIONS.
๐ ๏ธ I'd like to (temporarily) disable some of Embperl's features. What can be customized?
- Use
optDisableHtmlScanto disable processing of html tags. If this is set, Embperl will only pay attention to these types of constructs:
[+/-/!/$ .... $/!/-/+]
optDisableTableScan,optDisableInputScanandoptDisableMetaScancan be used to disable individual parts of HTML processing. You may set these flags in your server config, or at runtime:
[+ $optDisableHtmlScan = 1 +]
<table> foo </table>
[+ $optDisableHtmlScan = 0 +]
๐ How can I disable auto-tables?
Set optDisableTableScan in EMBPERL_OPTIONS
๐ How can I change predefined values like $escmode from my Toolbox module?
$HTML::Embperl::escmode = 0 ;
Predefined values in Embperl are simply aliases for $HTML::Embperl::foo (for instance, $escmode is an alias for $HTML::Embperl::escmode)
๐จ How can I customize the header that Embperl is sending?
You'll find the answer to this and many other header issues in the "Common Questions" section.
๐ค How can I use a different character set? ASCII values over 128 are showing up as ? (question marks)!
This is caused by the translation of characters to HTML escapes. Embperl translates them to escapes which are then sometimes not understood by the browser, which may display a "?" instead, because it is using the wrong character set.
If you want to use the escaping features of Embperl in this case, you have to adapt the file "epchar.c" to your character set.
The distribution contain already an "epchar.c.iso-latin-2" from Jiri Novak which is an replacement for epchar.c for the iso-8859-2 (iso-latin-2) character set. If you want to use iso-latin-2, simply renmae "epchar.c.iso-latin-2" to "epchar.c". There is also an file "epchar.c.min" from Sangmook Yi, which leaves all chars above 128 untouched, which is especially useful for two byte charsets.
This file contains three tables:
Char2Html []โ Convert characters to html escapeChar2Url []โ Convert characters to url escapes (do not change this one!!)Html2Char []โ Convert html escapes to characters
You need to change the first and the last tables. Do not change the second table!! ๐จ
Please make sure Char2Html contains one entry (and only one entry) for each of the 256 ascii codes (with none left undefined) in the right order, and that Html2Char is sorted by html escape.
If somebody generates new tables for national character sets, please send a copy to the author, so it can be included it in future versions of Embperl. ๐ซถ
โก Optimizing & Fine-Tuning
๐ง How can I be sure that Embperl is re-compiling my page template (and the Perl blocks contained in it) only when needed, and not each time?
As long as your input file's time stamp stays the same, Embperl will only compile the script the first time it's called. When you use the Execute function, Embperl will recompile the script only if the input file and mtime paramenters have changed since the last time the script was called.
You can verfiy this by setting dbgDefEval. Now, every time a Perl block is compiled, Embperl logs a line starting with DEF:. You will see this line only on the first request. The cached Perl blocks are stored as a set of subroutines in the namespace of the document. (HTML::Embperl::DOC::_<n> is the default) Look at the logfile to see the actual name.
๐๏ธ How can I pre-compile pages, so that each httpd child doesn't have to have its own separate copies of the pre-compiled pages?
To pre-compile pages, just call Execute once for every file at server startup in your startup.pl file.
๐ฆ In what namespace does Embperl store pre-compiled data?
The cached Perl blocks are stored as a set of subroutines in the namespace of the document. (HTML::Embperl::DOC::_<n> for default) Look at the logfile to see the actual name.
๐ค I have both Embperl and ordinary Perl processes running. The docs say that Embperl uses a CGI.pm instance in its own internal processing, but they don't say how to control it. How can I get Embperl to use my CGI.pm object instead of creating its own?
Embperl only creates a CGI objects to process multipart form data (from fileupload). In all other cases Embperl doesn't use CGI.pm. There is no way to change this behaviour, or access the internal CGI object in case of file-uploads. that's just how it is bestie ๐ญ
๐ Additional Help
๐ Where can I get more help?
You can get free support on the mod_perl mailing list. If you need commercial support (with a guarantee for response time or a solution) for Embperl, or if you want a web site where you can run your Embperl/mod_perl scripts without setting up your own web server, please send email to info@ecos.de.
Please also see the section "Support" in Embperl in the Embperl documentation.
๐ SEE ALSO
- some links here
๐ AUTHOR ๐
Okay so this page was written by the actual icon himself, Gerald Richter โ you can reach him at richter at embperl dot org ๐ซถ
And it got a glow-up edit from Nora Mikes โ hit her up at nora@radio.cz ๐
and that's the whole page, bestie!! ๐ you made it all the way to the end, which is main character energy fr fr ๐
this lil doc came straight from perl v5.34.0, dated 2022-02-06, and it lives as Faq(3pm) โจ
now go forth and slay your Embperl setup, ok? mwah ๐ฆ๐๐ธ
๐ฆ where this page comes from
- ubuntu jammy: libembperl-perl 2.5.0-16build1 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms