Is this considered ugly?

K

Kristo

I'm in the process of learning Perl and I'm wondering if there's a
preferred way to do the following. I have a list of strings that
potentially will contain ampersands. I'd like to replace all
occurrances of & with & for use on a webpage. Here's what I have
so far:

use warnings;
use strict;

my ($foo, $bar, $baz) = ("A&B", "C&D", "E&F");
foreach (($foo, $bar, $baz))
{
s/&/&/g;
}
print "Foo = $foo, Bar = $bar, Baz = $baz\n";
__END__

This outputs "Foo = A&B, Bar = C&D, Baz = E&F" as expected.
The reason I chose to name each element of the list was so I could use
the strings later and retain an idea of what each element represented.
Now, in an attempt to be more concise, I came up with this:

use warnings;
use strict;

my @foo = ("A&B", "C&D", "E&F");
foreach (@foo)
{
s/&/&/g;
}
print "Foo = $foo[0], Bar = $foo[1], Baz = $foo[2]\n";
__END__

I've succeeded in making the code shorter but at the cost of having to
remember which list element goes with each subscript. My question to
everyone is this: which approach is more "Perlish"?

Thanks in advance.

Kristo
 
P

Paul Lalli

Kristo said:
I'm in the process of learning Perl and I'm wondering if there's a
preferred way to do the following. I have a list of strings that
potentially will contain ampersands. I'd like to replace all
occurrances of & with & for use on a webpage. Here's what I have
so far:

use warnings;
use strict;

my ($foo, $bar, $baz) = ("A&B", "C&D", "E&F");
foreach (($foo, $bar, $baz))
{
s/&/&/g;
}
print "Foo = $foo, Bar = $bar, Baz = $baz\n";
__END__

This outputs "Foo = A&B, Bar = C&D, Baz = E&F" as expected.
The reason I chose to name each element of the list was so I could use
the strings later and retain an idea of what each element represented.
Now, in an attempt to be more concise, I came up with this:

use warnings;
use strict;

my @foo = ("A&B", "C&D", "E&F");
foreach (@foo)
{
s/&/&/g;
}
print "Foo = $foo[0], Bar = $foo[1], Baz = $foo[2]\n";
__END__

I've succeeded in making the code shorter but at the cost of having to
remember which list element goes with each subscript. My question to
everyone is this: which approach is more "Perlish"?

Neither. :)

Hashes are used exactly for this reason - to associate a string with the
value in that position of the structure.

my %strs = (Foo=>'A&B', Bar=>'C&D', Baz=>'E&F');
foreach (keys %strs){
$strs{$_} =~ s/&/&/g;
}
print join (', ', map "$_ = $strs{$_}", keys %strs), "\n";

Also, there already exists a function do do the substitution you're
looking for. It is CGI::escapeHTML. This will convert & to &amp;, < to
&lt;, etc:

#!/usr/bin/perl
use strict;
use warnings;
use CGI qw/escapeHTML/;
my %strs = (Foo=>'A&B', Bar=>'C&D', Baz=>'E&F', Omega=>'5<8');
foreach (keys %strs){
$strs{$_} = escapeHTML($strs{$_});
}
print join (', ', map "$_ = $strs{$_}", keys %strs), "\n";
__END__

For more information on what I've used here:
hashes: perldoc perldata
map: perldoc -f map
join: perldoc -f map
escapeHTML: perldoc CGI

Hope this helps,
Paul Lalli
 
J

John W. Krahn

Paul said:
I'm in the process of learning Perl and I'm wondering if there's a
preferred way to do the following. I have a list of strings that
potentially will contain ampersands. I'd like to replace all
occurrances of & with &amp; for use on a webpage. Here's what I have
so far:

use warnings;
use strict;

my ($foo, $bar, $baz) = ("A&B", "C&D", "E&F");
foreach (($foo, $bar, $baz))
{
s/&/&amp;/g;
}
print "Foo = $foo, Bar = $bar, Baz = $baz\n";
__END__

This outputs "Foo = A&amp;B, Bar = C&amp;D, Baz = E&amp;F" as
expected.

The reason I chose to name each element of the list was so I could use
the strings later and retain an idea of what each element represented.
Now, in an attempt to be more concise, I came up with this:

use warnings;
use strict;

my @foo = ("A&B", "C&D", "E&F");
foreach (@foo)
{
s/&/&amp;/g;
}
print "Foo = $foo[0], Bar = $foo[1], Baz = $foo[2]\n";
__END__

I've succeeded in making the code shorter but at the cost of having to
remember which list element goes with each subscript. My question to
everyone is this: which approach is more "Perlish"?


Neither. :)

Hashes are used exactly for this reason - to associate a string with the
value in that position of the structure.

my %strs = (Foo=>'A&B', Bar=>'C&D', Baz=>'E&F');
foreach (keys %strs){
$strs{$_} =~ s/&/&amp;/g;
}

And remember that hash keys can't be modified so this will work:

foreach (%strs)
{
s/&/&amp;/g;
}


$ perl -le'
my %strs = qw( Foo A&B Bar C&D Baz E&F );
print for %strs;
s/&/&amp;/g for %strs;
print for %strs;
'
Bar
C&D
Baz
E&F
Foo
A&B
Bar
C&amp;D
Baz
E&amp;F
Foo
A&amp;B




John
 
B

Big and Blue

Kristo said:
My question to
everyone is this: which approach is more "Perlish"?

Probably neither. On the assumption that other chars may end up there
which neeed to be handled the Perlish way to do things may be to use the
module which knows about them.

use HTML::Entities
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top