develooper Front page | perl.beginners | Postings from January 2002

RE: html entity conversion... one liner?

Thread Previous | Thread Next
John Edwards
January 31, 2002 06:44
RE: html entity conversion... one liner?
Message ID:
Why *must* it be a one liner. I think my suggestion is easier to understand
for someone having to maintain your code. If there is no other reason than
"I want a one liner" to do this on one line, then why not do it on two??
Just because it is possible in one line, doesn't mean it's the best

To me this:

$item = "<blah>";

$item  =~ s/</&lt;/g;
$item  =~ s/>/&gt;/g;

Seems far easier to understand than either this:

$item = "<blah>";

$item =~ s/([<>])/'&'. ($1 eq'<' ? 'l':'g') . 't;'/eg;

or this:

my %entity = (
        '<' => '&lt;',
        '>' => '&gt;',
        '&' => '&amp;',

$item = "<blah>";

$item =~ s/([<>&])/$entity{$1}/ge;

which isn't even a one liner as you have to define a lookup hash to begin

All IMHO ;)


-----Original Message-----
From: Briac Pilpré []
Sent: 31 January 2002 14:35
Subject: Re: html entity conversion... one liner?

On Thu, 31 Jan 2002 14:06:06 -0000, Michael Kavanagh
<> wrote:
> I thought it would be good to be able to do this:
> $item = "<blah>";
> $item  =~ tr/<>/(&lt;)(&gt;)/;
> to convert those <> symbols to their entity references. however, the
> tr operator doesn't seem to like using () to group... any comments on
> how to make this operation in to a one-liner?

Here's a possible suboptimal one-liner approach:

#!/usr/bin/perl -w
use strict;

my $item = "<gah>buh</gah> zoh";

$item =~ s/([<>])/'&'. ($1 eq'<' ? 'l':'g') . 't;'/eg;

print $item;


 << dynamic .sig on strike, we apologize for the inconvenience >>

To unsubscribe, e-mail:
For additional commands, e-mail:

This E-mail is confidential.  It should not be read, copied, disclosed or
used by any person other than the intended recipient.  Unauthorised use,
disclosure or copying by whatever medium is strictly prohibited and may be
unlawful.  If you have received this E-mail in error please contact the
sender immediately and delete the E-mail from your system.

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About