in reply to Performance Question

This is going to leave any <?--name_x--> item in your template unchanged if you don't have a defined value for it. Is that what you want?

You might look at using something like HTML::Template or Template Toolkit 2.

Replies are listed 'Best First'.
Re^2: Performance Question
by Anonymous Monk on Feb 28, 2007 at 16:09 UTC
    Yes, that's what I want, and definitely not any warning messages saying things like "Use of uninitialized value..."
      If you want to only swap in values that exist then use the following:
      s{(<\?--(\w+)-->)}{ defined($swap{$2}) ? $swap{$2} : $1 }eg;

      That will only replace values that match have a corresponding entry in %swap.

      If you are brave you can use something like this:
      s/<\?--(\w+)-->(??{ defined($swap{$^N}) ? '(?=)' : '(?!)' })/$swap{$1} +/g;

      It also does what you are looking for - but without the e modifier on the swap. You will probably need to read perlre to follow it fully - but essentially you are using the (??{}) block to check your regex as you go and then using ?= and ?! to report success or failure.

      my @a=qw(random brilliant braindead); print $a[rand(@a)];