[Cedarvalley] using subs with an s///g?
Stephen D. Wells
wells at cedarnet.org
Wed Dec 17 16:03:52 CST 2003
'e' is for execute or eval. The result is run as perl code and the
result of that code is replaced with the match.
example:
Uppercase the first letter of every word...
$string =~ s/(\b\w)/uc($1)/eg;
on a side note... you might want to be able to include attributes
to in your data and/or dynamically process subroutines... In that case
see if something like this looks interesting...
complete example
----------------
#!/usr/bin/perl -w
use strict;
while (<DATA>) {
no strict 'refs';
s/<!--\s+include:(.*?)\s+
(attrib:(["'])?(.*?)(["'])?)?
\s*-->/&$1($4)/gex;
print;
}
sub food {
my @attrib = ();
@attrib = split(/,/, $_[0]) if (defined($_[0]));
'called food ('. join("::", @attrib).')';
}
__DATA__
hello world
how are you today?
<!-- include:food attrib:"honest abe" -->
<!-- include:food -->
<!-- include:food attrib:watermellon,peanut,cabbage -->
<!-- include:food attrib:'peanut butter' -->
complete
[steve at dev steve]$ perl dat_rep.pl
hello world
how are you today?
called food (corn beef)
called food ()
called food (watermellon::peanut::cabbage)
called food (peanut butter)
complete
---------
You may need to tweak it depending on your needs.
STEVE
On Wed, 2003-12-10 at 17:47, Aaron Thompson wrote:
> Now to answer my own question...
>
> the fix:
>
> $data =~ s/<!-- include:gallery_table -->/display_gallery_list()/eg;
>
> ... anyone know what the 'e' does (other than what I want)?
>
> @
>
>
> On Wed, Dec 10, 2003 at 05:26:56PM -0600, Aaron Thompson wrote:
> > I would like to replace a string with the results of a sub
> > program the idea is as follows...
> >
> > I have a sub 'display_gallery_list' that returns an HTML table
> > listing some galleries... and I want to replace the string
> > '<!-- include:gallery_table -->' with the results of the sub.
> > Also this global replacement is preferred.
> >
> > something like this should be possible:
> >
> > $data =~ s/<!-- include:gallery_table -->/&display_gallery_list/g;
> >
> > I don't want to use the $` and $' variables...
> >
> > Any ideas/suggestions?
> >
> > @
> >
> > --
> > Aaron Thompson Unix Systems Administrator, College of Natural Science
> > University of Northern Iowa Cedar Falls, IA 50614
> >
> > "My doctor says that I have a malformed public-duty gland and a natural
> > deficiency in moral fiber, and that I am therefore excused from saving
> > Universes." -Douglas Adams
> > _______________________________________________
> > Cedarvalley mailing list
> > Cedarvalley at mail.pm.org
> > http://mail.pm.org/mailman/listinfo/cedarvalley
--
Stephen D. Wells <wells at cedarnet.org>
More information about the Cedarvalley
mailing list