[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

RE: [FWP] Q on Substitution improvement



Ok, I'm sure there's a better way (or at least TMTOWTDI :), but here's a
try, using an array of anonymous subs for the substitutions.

If you want more substitutions, just add more subroutines to the array...

It might be better to have different classes of arrays for different types
of substitutions, but I've got work to do :)

Oh, I forgot to check: there must be something in CGI.pm or a related module
that does this already, right?

	#!/usr/local/bin/perl -w


	# One entry per "easy" substitution
	my %easySubstitutions=( '>' => '>',
				'<' => '&lt;',
				'"' => '&quot;');


	# One subroutine per "complex" substitution, or any substitution
that 
	# introduces < or >, since they'd get wiped out by the later subs...
	my @subs=(
			sub { ${$_[0]}=~s/\r?\n/<br>/g; }
		);

	foreach(keys %easySubstitutions)
	{
		# unshift, so they come ahead of the '<' or '>'
introducers...
		unshift @subs, eval ('sub{
${$_[0]}=~s/'.$_.'/'.$easySubstitutions{$_}.'/g;}');
	}

	#This one has to come first, so it doesn't undo all the others...
	unshift @subs,  sub{ ${$_[0]}=~s/&/&amp;/g;};

	# Just to have something to test it on...
	my %cgiNameValues=(
		'Test',"Mike was here\nAnd there \"was Mike\"\r\n",
		'Test2','Mike is "here"',
		'test3',"x>y, except that\r\ny<=x!\n",
	);

	for my $k (keys %cgiNameValues)
	{
	#   $cgiNameValues{$_} =~ s/\n/<br>/g;      # change RETURNS to <br>
	#   $cgiNameValues{$_} =~ s/\"/\&quot;/g;   # Change quotes to HTML
code

	   print "Before, $k: $cgiNameValues{$k}\n";
	   foreach(@subs)
	   {
		# The braces around {$_} may be unecessary, but I'm
paranoid.
	   	&{$_}(\$cgiNameValues{$k});
	   }
	   print "After, $k: $cgiNameValues{$k}\n";
	}

> -----Original Message-----
> From:	Walter Torres [SMTP:walter@tscinternet.com]
> Sent:	Tuesday, June 22, 1999 11:36
> To:	fwp@technofile.org
> Subject:	[FWP] Q on Substitution improvement
> 
> I have this...
> 
> # ---------------------------------------------------------
> # We are going to run through each name/value pair sent up and
> # parse it to replace any RETURN (cr/lf) with <br>, the HTML line
> # break code and quotes with '&quot;'.
> for (keys %cgiNameValues)
> {
>    $cgiNameValues{$_} =~ s/\n/<br>/g;      # change RETURNS to <br>
>    $cgiNameValues{$_} =~ s/\"/\&quot;/g;   # Change quotes to HTML code
> }
> 
> OK, it works, but it doesn't feel right.
> 
> I also have a list of 50+ such swaps I need to make and I really don't
> think
> I need to create 50 such lines.
> 
> Isn't there a Perl-ish method to run down my 50+ hash and do the swap?
> 
> Thanks
> 
> Walter
> 
> 
> ==== Want to unsubscribe from Fun With Perl?
> ==== Well, if you insist... Send mail with body "unsubscribe" to
> ==== fwp-request@technofile.org

==== Want to unsubscribe from Fun With Perl?
==== Well, if you insist... Send mail with body "unsubscribe" to
==== fwp-request@technofile.org