File Coverage

blib/lib/WE_Content/PerlDD.pm
Criterion Covered Total %
statement 33 60 55.0
branch 8 22 36.3
condition n/a
subroutine 6 8 75.0
pod 0 4 0.0
total 47 94 50.0


line stmt bran cond sub pod time code
1             # -*- perl -*-
2              
3             #
4             # $Id: PerlDD.pm,v 1.7 2004/04/14 21:23:51 eserte Exp $
5             # Author: Slaven Rezic
6             #
7             # Copyright (C) 2001 Online Office Berlin. All rights reserved.
8             # Copyright (C) 2002 Slaven Rezic.
9             # This is free software; you can redistribute it and/or modify it under the
10             # terms of the GNU General Public License, see the file COPYING.
11              
12             #
13             # Mail: slaven@rezic.de
14             # WWW: http://we-framework.sourceforge.net
15             #
16              
17             package WE_Content::PerlDD;
18 2     2   1833 use base qw(WE_Content::Base);
  2         4  
  2         1313  
19              
20 2     2   16 use strict;
  2         6  
  2         156  
21 2     2   12 use vars qw($VERSION $consider_safe);
  2         4  
  2         513  
22             $VERSION = sprintf("%d.%02d", q$Revision: 1.7 $ =~ /(\d+)\.(\d+)/);
23              
24             $consider_safe = 0 if !defined $consider_safe;
25              
26             sub new {
27 1     1 0 29 my($class, %args) = @_;
28 1         2 my $self = {};
29 1         7 while(my($k,$v) = each %args) {
30 1 50       14 die "Option does not start with a dash: $k" if $k !~ /^-/;
31 1         7 $self->{ucfirst(substr($k,1))} = $v;
32             }
33 1         5 bless $self, $class;
34 1 50       12 if ($self->{File}) {
    0          
35 1         7 $self->parse(-file => $self->{File});
36             } elsif ($self->{String}) {
37 0         0 $self->parse(-string => $self->{String});
38             }
39 0         0 $self;
40             }
41              
42             sub parse {
43 1     1 0 4 my($self, %args) = @_;
44 1         10 my $buf = $self->get_string(%args);
45              
46 2     2   10 use vars qw($outdata $emptydata);
  2         3  
  2         1211  
47 1         2 $outdata = $emptydata = undef;
48              
49 1 50       4 if ($consider_safe) {
50 0         0 eval $buf;
51 0 0       0 if ($@) {
52 0         0 my $line = 1;
53 0         0 warn join("\n", map { sprintf("%3d: %s", $line++, $_) } split /\n/, $buf);
  0         0  
54 0         0 die $@;
55             }
56             } else {
57 1         2166 require Safe;
58 1         74239 undef $WE_Content::PerlDD::Safe::outdata;
59 1         3 undef $WE_Content::PerlDD::Safe::emptydata;
60 1         10 my $s = Safe->new("WE_Content::PerlDD::Safe");
61 1         3026 $s->reval($buf);
62 1 50       1128 if ($@) {
63 0         0 my $line = 1;
64 0         0 warn join("\n", map { sprintf("%3d: %s", $line++, $_) } split /\n/, $buf);
  0         0  
65 0         0 die $@;
66             }
67 1 50       12 if (defined $WE_Content::PerlDD::Safe::outdata) {
    50          
68 0         0 $outdata = $WE_Content::PerlDD::Safe::outdata;
69             } elsif (defined $WE_Content::PerlDD::Safe::emptydata) {
70 0         0 $emptydata = $WE_Content::PerlDD::Safe::emptydata;
71             }
72             }
73              
74 1 50       47 if (defined $outdata) {
    50          
75 0         0 $self->{Object} = $outdata;
76 0         0 $self->{Type} = 'content';
77             } elsif (defined $emptydata) {
78 0         0 $self->{Object} = eval $emptydata; # XXX should use Safe!
79 0         0 $self->{Type} = 'template';
80             } else {
81 1         227 die "No data found!";
82             }
83              
84 0           $self->{Object};
85             }
86              
87             sub serialize {
88 0     0 0   my $self = shift;
89 0           require Data::Dumper;
90 0           my $dd = Data::Dumper->new([$self->{Object}], ['outdata']);
91 0 0         if ($^O eq 'MSWin32') {
92 0           $dd->Indent(0); # to prevent a memory leak in ActivePerl 5.6.1
93             } else {
94 0           $dd->Indent(1);
95             }
96 0           $dd->Dump;
97             }
98              
99 0     0 0   sub ext { "pl" }
100              
101             1;
102              
103             __END__