Embperl FAQ - embed Perl code in your HTML docs
For basics on downloading, compiling, and installing, please see the "INSTALLATION" in INSTALL in the Embperl documentation. Please be sure to load Embperl at server startup - if you do not, various problems may result.
No.
Not at the present time, but this may change.
First mod_perl and Apache, then Embperl.
../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
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/
Try recompiling Perl and all modules -- this can sometimes make those annoying error messages disappear!
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:
NOTE: answer _no_ to mod_perl support. (This is important!)
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.
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).
#STATIC_EXTS = Apache Apache::Constants HTML::Embperl
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
perl Makefile.PL
make clean perl Makefule.PL make
(to compile in mod_perl support)
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:
make clean perl Makefile.PL make
(without this step, only the offline mode would be tested)
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 by PerlModule or in a PerlScript. See next question on how to do this.
You can load Embperl at server startup by PerlModule or in a startup.pl:
PerlModule HTML::Embperl
use HTML::Embperl
NOTE: Either of these approaches can often 'fix' SIGSEVs in any mod_perl handler, not just Embperl.
The most common problems of all involve Escaping and Unescaping. They are so common, that an entire section on "Escaping & Unescaping" is devoted to them.
[+ $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"...
This might be a problem with "Escaping & Unescaping" as well.
Sounds like a problem with Escaping & Unescaping again!
Unless, of course, you have already read the section on Escaping & Unescaping, and it is still happening... Like if you are using optRawInput and your 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).
If Embperl is not compiled at server startup, it can cause error messages, SEGfaults, core dumps, buffer overflow, etc - especially if you are 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".
This is a known problem, but it is 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)
You don't need PerlSendHeader when using Embperl - Embperl always sends its own httpd header.
You'll find the answer to this and many other header issues in the "Common Questions" section.
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.
Hey! Not you again!? I thought we already sent you to the "Escaping & Unescaping" section of the FAQ?!?! ;)
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.
The most common questions of all deal with "Escaping & Unescaping" - they are so common that the whole next section is devoted to them. Less common questions are addressed here:
ePerl also embeds Perl code in documents, but it doesn't know anything about HTML.
Embperl's strongest features (the ones which have received the most attention during development so far) are probably:
This is a very good example of a very long run-on sentence, and it is also a very good example of a thing that ePerl would do better than Embperl.
See ePerl, by Ralf Engelschalls, at http://www.engelschall.com. See also the previous question, "What is the difference between Embperl and ePerl?"
You can 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 you can use
[- $req_rec -> content_type ('text/html') -]
Yes. Embperl sends its own headers, so all you have to do to send cookies is to remember to print an additional header.
Example Code:
<META HTTP-EQUIV="Set-Cookie" CONTENT="[+ $cookie +] = [+ $value +]">
[- $req_rec -> header_out("Set-Cookie" => "$cookie=$value"); -]
The function you want to call is Apache::header_out.
[- use Apache; use Apache::Constants qw(REDIRECT); $req_rec->header_out("Location" => "http://$ENV{SERVER_NAME}/specials/"); $req_rec->status(REDIRECT); -]
As always, there is more than one way to do this - especially as this is more of a question of how you are coding your HTML than how you are coding your Embperl.
Here are some ideas:
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...
Yes. 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">.
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">
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.
Embperl will be converted to an Object in the near future. This will, 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... :)
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.
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.
By default, Embperl removes all HTML tags from the Perl source. It does this because many high-end WYSIWYG HTML Editors (like MS Front Page) insert HTML tags like <FONT> and <COLOR> in rather random places (like in the middle of your Perl code). This Embperl feature keeps things like
[- $var = 1; <br> $foo = 2 -]
permissable, so that you can enter Perl code while you mark 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 are the kind of person who codes HTML in an ascii editor (like vi or emacs).
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.
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.
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>"
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>
<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.
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.
(You need a double backslash \\, because the first one is removed by Perl and the second by Embperl.
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
For more on using HTML links to the Embperl error log, see "EMBPERL_DEBUG" in Embperl in the Embperldocs.
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!
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.
[+/-/!/$ .... $/!/-/+]
You may set these flags in your server config, or at runtime:
[+ $optDisableHtmlScan = 1 +] <table> foo </table> [+ $optDisableHtmlScan = 0 +]
Set optDisableTableScan in EMBPERL_OPTIONS
$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)
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. This file contains three tables:
Char2Html [] Convert characters to html escape
Char2Url [] 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.
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.
To pre-compile pages, just call Execute once for every file at server startup in your startup.pl file.
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.
Embperl only creates a CGI objects to process multipart form data (from fileupload). In all other cases Embperl doesn't use CGI.pm
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.
some links here
Gerald Richter <richter@ecos.de>
Edited by Nora Mikes <nora@radio.cz>
To install lib, copy and paste the appropriate command in to your terminal.
cpanm
cpanm lib
CPAN shell
perl -MCPAN -e shell install lib
For more information on module installation, please visit the detailed CPAN module installation guide.