On Wed, 27 Aug 2003, Dave Cash wrote:

> Have you looked at the Data::Dumper plugin (Dumper) for TT2?  

yes, I'm using Data::Dumper routinely, I needed something different. 

BTW, it would be nice to have a way of importing Dumper structure in tt2, 
I'm using a modified version of tpage to do that (and also xml and
storable files) in a safe way, but it could be passed as a method:

use Safe;
use XML::Simple; 
use Storable qw/nstore_fd/;

my $forcearray=1;
my $forcecontent=1;
my $DEBUG=1;


sub load {
  my $compartment = Safe->new('Load'); 
  my $load = $_[1];
  
  unless (-e $load) {
    die "Error: --load requires a valid file (you wrote \"$load\")";
  }
  
  if ($load =~ /\.pl$/) { # perl file
    print STDERR "load: found perl file $load\n" if $DEBUG;
    $compartment->rdo($load) or die "load error: [EMAIL PROTECTED]";
  } elsif ($load =~ /\.xml$/) { #xml file
    if ($DEBUG) {
      print STDERR "load: found xml file $load\n
      forcearray: $forcearray\n
      forcecontent: $forcecontent\n";
    } 
    
    $Load::VAR1=XMLin(
      $load,
      searchpath=>'.',
      forcearray=>$forcearray,
      forcecontent=>$forcecontent, 
      cache=>'storable',
      parseropts => [ProtocolEncoding => 'ISO-8859-1'],
    );
  } elsif ($load =~ /\.stor$/) { # storable
    if ($DEBUG) {
      print STDERR "load: found storable file $load\n";
    }
    $Load::VAR1 = retrieve ($load);
  } else {
    die "error: format not yet implemented";
  }
  # import hashes from $Load::
  foreach my $hash (keys %Load::) {
    if (defined %${$Load::{$hash}} and $hash ne "main::") {
      print STDERR "importing $hash\n" if $DEBUG;
      addkeys($vars, ${$Load::{$hash}});
  }
  print STDERR "load: ended\n" if $DEBUG;
}
 
sub addkeys {
  my ($dest, $src) = @_;
  my ($key, $value);
  while ( ($key,$value) = each(%{$src}) ) {
    $dest->{$key} = $value;
  }
}

-- 
Franco Bagnoli (franchino) <[EMAIL PROTECTED]> ([EMAIL PROTECTED])
virtual location: Dipartimento di Energetica "S. Stecco"
real location: Dip. Matematica Applicata "G. Sansone", Universita' Firenze,
Via S. Marta, 3 I-50139 Firenze, Italy. Tel. +39 0554796422, fax: +39 055471787
GPG Key fingerprint = 169D 9EA5 8FD3 7EDA E43A  9830 255F BCEC 0D63 3728


_______________________________________________
templates mailing list
[EMAIL PROTECTED]
http://lists.template-toolkit.org/mailman/listinfo/templates

Reply via email to