[Israel.pm] Perl Advocacy

Zohar Kelrich lumi at musicgenome.com
Mon Dec 27 10:59:01 PST 2004


On Mon, 27 Dec 2004 16:31:26 +0200, david istermann <interdist at gmail.com>  
wrote:

> What bugs me most is that in Perl there is no equivalent for PHP's  
> str_replace:
>
> $newstr = str_replace( array('<', '>', '&'), array('&lt;', '&gt;',
> '&amp;'), $oldstr);

Is there anything wrong with:
8<----
sub strreplc {
	my $reps=shift;
	my $str = shift;
	my @vals = keys %{$reps};
	my $s=join'|', sort {length $a <=> length $b} map {quotemeta} keys  
%{$reps};
	$str =~ s/($s)/$reps->{$1}/g;
	return $str;
}

---->8

for example?

I think it should work for anything the original one works for, and its  
name is 3 characters shorter!



More information about the Perl mailing list