[Templates-cvs] cvs commit: TT3/lib/Template Base.pm

cvs@template-toolkit.org cvs@template-toolkit.org
Sun, 12 Dec 2004 15:50:01 +0000


cvs         04/12/12 15:50:00

  Modified:    lib/Template Base.pm
  Log:
  * fixed some code in throw()
  * deprecated module() and object() methods
  
  Revision  Changes    Path
  1.15      +37 -30    TT3/lib/Template/Base.pm
  
  Index: Base.pm
  ===================================================================
  RCS file: /template-toolkit/TT3/lib/Template/Base.pm,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- Base.pm	2004/12/10 18:34:46	1.14
  +++ Base.pm	2004/12/12 15:50:00	1.15
  @@ -16,7 +16,7 @@
   #   modify it under the same terms as Perl itself.
   #
   # REVISION
  -#   $Id: Base.pm,v 1.14 2004/12/10 18:34:46 abw Exp $
  +#   $Id: Base.pm,v 1.15 2004/12/12 15:50:00 abw Exp $
   #
   #========================================================================
   
  @@ -29,7 +29,7 @@
   require Template::Utils;
   require Template::Exception;
   
  -our $VERSION   = sprintf("%d.%02d", q$Revision: 1.14 $ =~ /(\d+)\.(\d+)/);
  +our $VERSION   = sprintf("%d.%02d", q$Revision: 1.15 $ =~ /(\d+)\.(\d+)/);
   our $DEBUG     = 0 unless defined $DEBUG;
   our $ERROR     = '';
   our $PAD       = '  ';
  @@ -269,6 +269,9 @@
           # throw error or return undef/0 value of $throw
           if ($throw) {
               return $self->throw($throw, $error);
  +            #my ($pkg, $file, $line) = caller(0);
  +            #$self->debug("error() caller: $file at line $line\n");
  +            #return $self->throw($throw, $error, file => $file, line => $line);
           }
           else {
               return $throw;
  @@ -322,42 +325,38 @@
   
   sub throw {
       my $self = shift;
  +    my $type = shift;
   
  -    if (@_ == 1) {
  -        # if we got one argument then it's either an exception object
  -        # which can be throw, or an error message which is first 
  -        # upgraded to an 'undef' exception
  +    if (@_) {
  +        my $info = shift;
  +        my $config = @_ && UNIVERSAL::isa($_[0], 'HASH') ? shift : { @_ };
   
  -        if (UNIVERSAL::isa($_[0], $EXCEPTION)) {
  -            die $_[0];
  +        if (UNIVERSAL::isa($info, $EXCEPTION) && $info->type() eq $type) {
  +            # second argument is already an exception of type $type
  +            die $info;
           }
           else {
  -            die $EXCEPTION->new(undef => $_[0]);
  +            # construct a new exception from $type and $info fields
  +            $config->{ type } = $type;
  +            $config->{ info } = $info;
  +            $self->debug("throwing new exception object: ", 
  +                         $self->dump_hash($config), "\n") if $DEBUG;
  +            die $EXCEPTION->new($config);
           }
       }
  -    elsif (@_ == 2) {
  -        # if we got two arguments then it's either type and info, which 
  -        # we turn into an exception, or it's a type and exception object.
  -        # in this case we throw the existing exception if it has the same
  -        # type as the one we're expecting to throw, otherwise we create 
  -        # a new exception and pass the old one as the info field
  -
  -        if ( UNIVERSAL::isa($_[1], $EXCEPTION) && $_[1]->type() eq $_[0]) {
  -            die $_[1];
  +    else {
  +        # single argument can be an exception object or an error message
  +        if (UNIVERSAL::isa($type, $EXCEPTION)) {
  +            $self->debug("re-throwing exception object: ", ref $type, " => $type\n")
  +                if $DEBUG;
  +            die $type;
           }
           else {
  -            die $EXCEPTION->new(@_);
  +            $self->debug("throwing new exception object: info => $type\n")
  +                if $DEBUG;
  +            die $EXCEPTION->new( info => $type );
           }
       }
  -    elsif (@_) {
  -        # if we get more than two arguments then we need to create a new
  -        # exception to store the extra arguments, regardless of whether 
  -        # or not any exception object passed already has the correct type.
  -        die $EXCEPTION->new(@_);
  -    }
  -    else {
  -        die "throw() called without any arguments\n";
  -    }
   }
   
   
  @@ -478,6 +477,10 @@
   
   sub module {
       my $self = shift;
  +
  +    my ($pkg, $file, $line) = caller(0);
  +    die "module() has been deprecated, please fix the code at $file line $line\n";
  +
       my $name = shift;
       my $config = @_ && UNIVERSAL::isa($_[0], 'HASH') ? shift : { @_ };
   
  @@ -528,6 +531,10 @@
   sub object {
       my $self = shift;
       my $name = shift;
  +
  +    my ($pkg, $file, $line) = caller(0);
  +    die "object() has been deprecated, please fix the code at $file line $line\n";
  +
       my $module = $self->module( $name ) || return;
   
       # module may already be an object
  @@ -593,7 +600,7 @@
       $indent ||= 0;
       my $pad = $PAD x $indent;
       
  -    return '{ }' unless %$hash;
  +    return '{ }' unless $hash && %$hash;
       return "\{\n" 
           . join( ",\n", 
                   map { "$pad$PAD$_ => " . $self->dump_item($hash->{$_}, $indent + 1) }
  @@ -1174,7 +1181,7 @@
   
   =head1 VERSION
   
  -$Revision: 1.14 $
  +$Revision: 1.15 $
   
   =head1 COPYRIGHT