control over encoding

Benjamin Tucker <[email protected]> Thu, 27 Jul 2006 02:37:02 -0400
Newsgroups gmane.comp.lang.perl.modules.petal
Message-ID <[email protected]>
--Apple-Mail-3--418636591
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
	charset=US-ASCII;
	delsp=yes;
	format=flowed

Hi all,

I'm new to Petal and the list, so I apologize if this is something  
that's been discussed before.  I spent the better part of today  
integrating Petal into a mod_perl-based CMS.  All went very well with  
one exception.  There are a number of methods I had wanted to expose  
to the templates which return HTML.  Of course Petal helpfully  
encodes them.  I know that it provides the 'structure' keyword to  
turn this off, but I didn't want template authors to be forced to  
think about it, and rather wanted control from the data side of when  
escaping should not occur.

So my solution was I changed all the exposed methods that returned  
HTML to return the string as a scalar ref.  next I changed  
Petal::Hash::Var::process to not dereference scalar refs.  then I  
added to Petal::Hash::get_encoded logic not to not encode if $res is  
a scalar reference, and then dereferences $res.  (see attached patches).

Will this break anything?  is Petal::Hash::Var::process ever accessed  
without going through Petal::Hash::get_encoded?  Does anyone have a  
suggestion of a better solution.

Thanks!
Ben


--Apple-Mail-3--418636591
Content-Transfer-Encoding: 7bit
Content-Type: application/octet-stream;
	x-unix-mode=0644;
	name=Hash.pm.diff
Content-Disposition: attachment;
	filename=Hash.pm.diff

--- /Users/btucker/.cpan/build/Petal-2.19/lib/Petal/Hash.pm	2006-01-27 08:02:30.000000000 -0500
+++ Petal/Hash.pm	2006-07-27 01:57:44.000000000 -0400
@@ -129,9 +129,14 @@
     my $key  = shift;
     my $res  = $self->get ($key);
     return unless (defined $res);
+    my $no_encode;
+    if ($no_encode = ref $res eq "SCALAR")
+    {
+      $res = $$res;
+    } 
+    $no_encode ||= $key =~ s/^\s*structure\s+//;
 
-    my $no_encode = $key =~ s/^\s*structure\s+//;
-    unless ($no_encode and $no_encode)
+    unless ($no_encode)
     {
         $res =~ s/\&/\&amp;/g;
         $res =~ s/\</\&lt;/g;

--Apple-Mail-3--418636591
Content-Transfer-Encoding: 7bit
Content-Type: application/octet-stream;
	x-unix-mode=0644;
	name=Var.pm.diff
Content-Disposition: attachment;
	filename=Var.pm.diff

--- /Users/btucker/.cpan/build/Petal-2.19/lib/Petal/Hash/Var.pm	2006-03-23 12:20:38.000000000 -0500
+++ Petal/Hash/Var.pm	2006-07-27 01:58:20.000000000 -0400
@@ -136,7 +136,6 @@
     
     # return '' unless (defined $current);
     # $current = "$current" if (defined $current);
-    return $$current if isa($current, 'SCALAR');
     return $current;
 }
 

--Apple-Mail-3--418636591--