Hi All, hi Gerald,

I wrote some code for a forum, that displays user entered text,
turning all URLs found into real links. Here it is:

[- @t = split(/\x0d\x0a/, $message); $r = '';
   {
     local $/ = "\x0d\x0a"; # for chomp below
     for($i=0;$i < scalar @t;$i++) {
       $out = ''; $t = $t[$i];
       HTML::Embperl::Execute({'escmode' => 1,
                               'input' => \$t,
                               'output' => \$out,
                               'mtime' => undef});
       chomp $out;
       $out =~ s@((?:ftp|http|news)://[^ ]*[^ .,:;!?<>()])@<A
HREF="$1">$1</A>@gi;
       $r .= "<BR>\n" if $r;
       $r .= $out;
     }
   } -]
   [+ local $escmode=0; $r +]


It may not be optimal, but it works well. I tried to put that code in
a Perl module, since it looks quite reusable:

package XXX;
use strict;
use lib qw(..);
use Exporter ();
use HTML::Embperl ();
use vars qw(@ISA @EXPORT_OK);
@ISA = qw(Exporter);
@EXPORT_OK = qw(UserText);

sub UserText($) {
  my($Text) = @_;
  {
    local $/ = "\x0d\x0a"; # for chomp below
    return join("<BR>\n",
      map { my $out;
            HTML::Embperl::Execute({'escmode' => 1,
                                    'input' => \$_,
                                    'output' => \$out,
                                    'mtime' => undef});
            chomp $out;
            $out =~ s@((?:ftp|http|news)://[^ ]*[^ .,:;!?<>()])@<A
HREF="$1">$1</A>@gi;
            $out }
          split(/\x0d\x0a/, $Text)
        );
  }
}

1;

When called from command-line using a Perl script, all seems fine.
>From a HTML page, code becomes :

[- use XXX qw(UserText); -]
...
[+ local $escmode=0; UserText($message) +]
...

But now, if $message contains several lines, UserText($message)
contains the conversion of the first line of $message, several times.
It looks like HTML::Embperl::Execute() only modifies $out during the
first loop.

What am I missing ?


-- 
Linux blade.workgroup 2.4.0-test10 #1 Thu Dec 7 12:47:22 CET 2000 i686 unknown
 10:20am  up 1 day, 14:50,  8 users,  load average: 1.34, 1.73, 1.55


-- 
Linux blade.workgroup 2.4.0-test10 #1 Thu Dec 7 12:47:22 CET 2000 i686 unknown
  4:09pm  up 1 day, 20:40,  7 users,  load average: 1.16, 1.15, 1.18

Reply via email to