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

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


cvs         04/12/12 15:50:40

  Modified:    lib/Template Exception.pm
  Log:
  * changed Template::Exception to use named parameters like everything
    else does
  
  Revision  Changes    Path
  1.5       +39 -41    TT3/lib/Template/Exception.pm
  
  Index: Exception.pm
  ===================================================================
  RCS file: /template-toolkit/TT3/lib/Template/Exception.pm,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- Exception.pm	2004/12/10 12:50:07	1.4
  +++ Exception.pm	2004/12/12 15:50:40	1.5
  @@ -16,7 +16,7 @@
   #   modify it under the same terms as Perl itself.
   #
   # REVISION
  -#   $Id: Exception.pm,v 1.4 2004/12/10 12:50:07 abw Exp $
  +#   $Id: Exception.pm,v 1.5 2004/12/12 15:50:40 abw Exp $
   #
   # TODO
   #   Decide if exceptions still need to handle text buffers and how
  @@ -31,51 +31,28 @@
   use Template::Base;
   use base qw( Template::Base );
   
  -our $VERSION = sprintf("%d.%02d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/);
  +our $VERSION = sprintf("%d.%02d", q$Revision: 1.5 $ =~ /(\d+)\.(\d+)/);
   our $DEBUG   = 0 unless defined $DEBUG;
   our $ERROR   = '';
   our $FORMAT  = '<type> error - <info>' unless defined $FORMAT;
  -our $DEFAULT = {
  -    type => 'undef',
  -    info => '(no information)',
  -} unless defined $DEFAULT;
  +our $TYPE    = 'undef' unless defined $TYPE;
   
   use overload 
       q|""|    => "text", 
       fallback => 1;
   
   
  -#------------------------------------------------------------------------
  -# new($type, $info)
  -#------------------------------------------------------------------------
   
  -sub new {
  -    my $class = shift;
  -    my ($type, $info, $text);
  +sub init {
  +    my ($self, $config) = @_;
   
  -    if (@_ && UNIVERSAL::isa($_[0], 'HASH')) {
  -        my $hash = shift;
  -        ($type, $info, $text) = @$hash{ qw( type info text ) };
  -    }
  -    else {
  -        ($type, $info, $text) = @_;
  -    }
  -        
  -    my $default;
  +    $self->{ type } = $config->{ type } || $self->pkgvar( TYPE => $TYPE );
  +    $self->{ info } = $config->{ info } || '';
  +    $self->{ text } = $config->{ text } || '';
  +    $self->{ file } = $config->{ file };
  +    $self->{ line } = $config->{ line };
   
  -    # look for $DEFAULT hash in derived package 
  -    no strict 'refs';
  -    $default = ${"$class\::DEFAULT"} || $DEFAULT;
  -
  -    $type = $default->{ type } 
  -        unless defined($type) and length($type);
  -    $info = $default->{ info } 
  -        unless defined($info) and length($info);
  -
  -    bless {
  -        type => $type,
  -        info => $info,
  -    }, $class;
  +    return $self;
   }
   
   
  @@ -104,6 +81,30 @@
   
   
   #------------------------------------------------------------------------
  +# file()
  +#
  +# Get/set the exception file information.
  +#------------------------------------------------------------------------
  +
  +sub file {
  +    my $self = shift;
  +    return @_ ? ($self->{ file } = shift) : ($self->{ file } || 'unknown');
  +}
  +
  +
  +#------------------------------------------------------------------------
  +# line()
  +#
  +# Get/set the line number.
  +#------------------------------------------------------------------------
  +
  +sub line {
  +    my $self = shift;
  +    return @_ ? ($self->{ line } = shift) : ($self->{ line } || 'unknown');
  +}
  +
  +
  +#------------------------------------------------------------------------
   # text()
   #
   # Return a text string containing the type and info fields.
  @@ -111,14 +112,11 @@
   
   sub text {
       my $self   = shift;
  -    my $format = shift || do {
  -        # look for $FORMAT hash in derived package 
  -        no strict 'refs';
  -        my $class = ref $self || $self;
  -        ${"$class\::FORMAT"} || $FORMAT;
  -    };
  +    my $format = shift || $self->pkgvar( FORMAT => $FORMAT );
   
       $format =~ s/<(\w+)>/defined $self->{ $1 } ? $self->{ $1 } : "(no $1)"/eg;
  +    $format .= " in $self->{ file }" if $self->{ file };
  +    $format .= " at line $self->{ line }" if $self->{ line };
   
       return $format;
   }
  @@ -287,7 +285,7 @@
   
   =head1 VERSION
   
  -$Revision: 1.4 $
  +$Revision: 1.5 $
   
   =head1 COPYRIGHT