Mailing List Archive

A bunch of glitches I'd like some help with
Dear all -

I'm just getting into EmbPerl (very nice!), but I'm coming across a few
difficulties. I'm wondering if there is a better way of doing what I want
to do. I'm using version 1.2.1 on Debian, with Apache mod_perl.


(1) I have some fields that contain multi-line data (i.e. with embedded '\n'
newlines). To display these properly, I need to insert a <br> tag for each
'\n'. I can do a `s/\n/<br>/sg;', but I now I have two alternatives,
neither of which work:

- Output with $escmode==0, but then all my [<>&] characters are wrong
- Output with $escmode==3, but then I just get 4-characters "<br>" where
I want my tags to be

Maybe one solution would be to manually do the [<>&] conversion, then
substitute the <br>, and output $escmode==0, which is a bit of a mess. What
I ended up with was this, which isn't much better, putting an extra <br> on
the end, but it works for now:

<td>
[$ foreach $lin (split(/\n/, $dat[$row])) $]
[+$lin+]<br>
[$ endforeach $]
</td>

Is there a better way to do this that I've missed ?


(2) I want to turn on $optEarlyHeaders just for one or two scripts (not a
whole directory). There doesn't seem to be a way to do this (unless I've
missed it). What would be ideal (from my point of view) would be to have a
call FlushOutput(), or something like that, that would flush the headers and
all the buffered output out to the browser, and from then on go into
$optEarlyHeaders mode. This would be particularly useful for pages that
generate output for the user, and then do a lot of background updates
(mail-sending, whatever) before finishing. We don't have to make the user
wait for all that to finish. Is there some way of doing this that I've
missed ?


(3) The last thing was that I wanted to define a subroutine to call in case
of fatal errors, to generate some output for the user, then abort the
script. However, all my attempts to end the routine with exit(0),
Apache::exit(0) or even `die', were not working, and the routine kept on
returning and continuing the execution.

This is what I was doing (more or less):

[$ sub error $]
There was an error: [+ $_[0] +]
[- Apache::exit(0); -]
[$ endsub $]

...

[-
do_something() || error("Can't do_something");
-]

That didn't work. However, using a [! !] block for the subroutine
containing the exit() call *did* work:

[! sub error { disp_error($_[0]); Apache::exit(0); } !]
[$ sub disp_error $]
There was an error: [+ $_[0] +]
[$ endsub $]

I did actually try putting the subroutine in a [- -] block initially, and
that didn't work either, but I can't get it to go wrong in the simple case
now - maybe it's connected with [$ if $] blocks as well. I can try to
reproduce that example again if this is important to find a bug.

This is all a bit weird, and I'm sure there is a good reason for it. Is
this the intended behaviour for exit(), Apache::exit() and die ? If so, it
would be useful to have some discussion on this in the documentation - on
how to successfully abort the script, and why what I tried wasn't working.
This is quite confusing.


Thanks -

Jim

--
Jim Peters / __ | \ Aguazul
/ /| /| )| /| / )|| \
jim@aguazul. \ (_|(_|(_|(_| )(_|I / www.aguazul.
demon.co.uk \ ._) _/ / demon.co.uk
RE: A bunch of glitches I'd like some help with [ In reply to ]
>
> (1) I have some fields that contain multi-line data (i.e. with
> embedded '\n'
> newlines). To display these properly, I need to insert a <br>
> tag for each
> '\n'. I can do a `s/\n/<br>/sg;', but I now I have two alternatives,
> neither of which work:
>

You need to escape the html tag:

[+ $foo =~ s/\n/\\\\<br\\>/g; $foo +]

or if you have optRawInput off:

[+ $foo =~ s/\n/\\<br\\>/g; $foo +]

(and use $escmode = 3, the default )

>
> (2) I want to turn on $optEarlyHeaders just for one or two scripts (not a
> whole directory). There doesn't seem to be a way to do this (unless I've
> missed it).

Put a Files block inside your Location/Directory block that enables Embperl:

<Files file1.epl>
PerlSetEnv EMBPERL_OPTIONS 64
</Files>
<Files file2.epl>
PerlSetEnv EMBPERL_OPTIONS 64
</Files>


> What would be ideal (from my point of view) would be
> to have a
> call FlushOutput(),

Yes, that would be nices, but isn't so easy to implement. I put it on the
TODO list

>
> (3) The last thing was that I wanted to define a subroutine to
> call in case
> of fatal errors, to generate some output for the user, then abort the
> script. However, all my attempts to end the routine with exit(0),
> Apache::exit(0) or even `die',

die only terminates the current [-/+ ... -/+] block

> were not working, and the routine kept on
> returning and continuing the execution.
>
> This is what I was doing (more or less):
>
> [$ sub error $]
> There was an error: [+ $_[0] +]
> [- Apache::exit(0); -]
> [$ endsub $]
>
> ...
>
> [-
> do_something() || error("Can't do_something");
> -]
>

The above code should work (also you need only to write exit(0)). One thing
is that exit only terminates the current file, so if you call this file from
another one via Execute, the other one will continue to run.

> That didn't work. However, using a [! !] block for the subroutine
> containing the exit() call *did* work:
>
> [! sub error { disp_error($_[0]); Apache::exit(0); } !]
> [$ sub disp_error $]
> There was an error: [+ $_[0] +]
> [$ endsub $]
>

This is strange, both should do the same...

>
> This is all a bit weird, and I'm sure there is a good reason for it. Is
> this the intended behaviour for exit(), Apache::exit() and die ?
> If so, it
> would be useful to have some discussion on this in the documentation

There is already something about exit:

http://perl.apache.org/embperl/Embperl.pod.9.html#exit

- exit

exit will override the normal Perl exit in every Embperl document. Calling
exit will immediately stop any further processing of that file and send the
already-done work to the output/browser.

NOTE 1: If you are inside of an Execute, Embperl will only exit this
Execute, but the file which called the file containing the exit with Execute
will continue.

NOTE 2: If you write a module which should work with Embperl under mod_perl,
you must use Apache::exit instead of the normal Perl exit (just like always
when running under mod_perl).


If you, after solving your problem, have any ideas how to write it more
clear, please send me a patch

Gerald



-------------------------------------------------------------
Gerald Richter ecos electronic communication services gmbh
Internetconnect * Webserver/-design/-datenbanken * Consulting

Post: Tulpenstrasse 5 D-55276 Dienheim b. Mainz
E-Mail: richter@ecos.de Voice: +49 6133 925151
WWW: http://www.ecos.de Fax: +49 6133 925152
-------------------------------------------------------------
Re: A bunch of glitches I'd like some help with [ In reply to ]
> > (1) I have some fields that contain multi-line data (i.e. with
> > embedded '\n'
>
> You need to escape the html tag:
>
> [+ $foo =~ s/\n/\\\\<br\\>/g; $foo +]

This works fine now, thanks.

> > This is what I was doing (more or less):
> >
> > [$ sub error $]
> > There was an error: [+ $_[0] +]
> > [- Apache::exit(0); -]
> > [$ endsub $]
> >
> > ...
> >
> > [-
> > do_something() || error("Can't do_something");
> > -]
> >
>
>
> > That didn't work. However, using a [! !] block for the subroutine
> > containing the exit() call *did* work:
> >
> > [! sub error { disp_error($_[0]); Apache::exit(0); } !]
> > [$ sub disp_error $]
> > There was an error: [+ $_[0] +]
> > [$ endsub $]
> >
>
> This is strange, both should do the same...

This is very very strange - all of my problems regarding exit() have now
completely vanished. I tried the above examples again, and they now work. I
wonder if there was something strange in my configuration that triggered it.
If it happens again, I'll try and find what caused it.

> If you, after solving your problem, have any ideas how to write it more
> clear, please send me a patch

It's fine now it's agreeing with what I see before my eyes !

Thanks -

Jim

--
Jim Peters / __ | \ Aguazul
/ /| /| )| /| / )|| \
jim@aguazul. \ (_|(_|(_|(_| )(_|I / www.aguazul.
demon.co.uk \ ._) _/ / demon.co.uk