in reply to Re: Re: how do I use s///e
in thread how do I use s///e

I realize your code was mainly for demonstration purposes, but it can be shortened considerably. I'm sure this can be golfed down even more, but I came up with the following snippet, which I feel is simple yet not too obfuscated (and also demonstrates the use of s///e, so I guess it's not really too OT):

#!/usr/bin/perl use strict; use warnings; my $str = 'The %NOUN% can jump very %ADVERB% with his %APPENDAGES%'; my %wb = ( NOUN => "kangaroo", ADVERB => "far", ); $str =~ s/%([^%]+)%/$wb{$1} || "\%$1\%"/ge; print "$str\n";

Update: %s/\$template/\$str/g; silly mistake caused by last-minute-edits-without-testing. Thanks to Not_a_Number for the catch.

Replies are listed 'Best First'.
Re: Re: Re: Re: how do I use s///e
by perlfan (Parson) on Jan 29, 2004 at 21:58 UTC
    Thanks :)