[Templates-cvs] cvs commit: Template2/lib/Template Constants.pm Stash.pm

cvs@template-toolkit.org cvs@template-toolkit.org


cvs         06/05/25 11:23:36

  Modified:    lib/Template Constants.pm Stash.pm
  Log:
  Added code to replace virtual method to bypass slow code if no backrefs
  are defined
  
  Revision  Changes    Path
  2.71      +8 -8      Template2/lib/Template/Constants.pm
  
  Index: Constants.pm
  ===================================================================
  RCS file: /template-toolkit/Template2/lib/Template/Constants.pm,v
  retrieving revision 2.70
  retrieving revision 2.71
  diff -u -r2.70 -r2.71
  --- Constants.pm	2006/05/25 08:22:39	2.70
  +++ Constants.pm	2006/05/25 11:23:35	2.71
  @@ -9,29 +9,29 @@
   #   Andy Wardley   <abw@kfs.org>
   #
   # COPYRIGHT
  -#   Copyright (C) 1996-2000 Andy Wardley.  All Rights Reserved.
  +#   Copyright (C) 1996-2006 Andy Wardley.  All Rights Reserved.
   #   Copyright (C) 1998-2000 Canon Research Centre Europe Ltd.
   #
   #   This module is free software; you can redistribute it and/or
   #   modify it under the same terms as Perl itself.
   #
  -#----------------------------------------------------------------------------
  +# REVISION
  +#   $Id: Constants.pm,v 2.71 2006/05/25 11:23:35 abw Exp $
   #
  -# $Id: Constants.pm,v 2.70 2006/05/25 08:22:39 abw Exp $
  -#
   #============================================================================
    
   package Template::Constants;
   
  -require 5.004;
   require Exporter;
   
   use strict;
  -use vars qw( $VERSION @ISA @EXPORT_OK %EXPORT_TAGS );
  +use warnings;
  +use base 'Exporter';
  +
  +use vars qw( @EXPORT_OK %EXPORT_TAGS );
   use vars qw( $DEBUG_OPTIONS @STATUS @ERROR @CHOMP @DEBUG);
   
  -@ISA     = qw( Exporter );
  -$VERSION = sprintf("%d.%02d", q$Revision: 2.70 $ =~ /(\d+)\.(\d+)/);
  +our $VERSION = sprintf("%d.%02d", q$Revision: 2.71 $ =~ /(\d+)\.(\d+)/);
   
   
   #========================================================================
  
  
  
  2.102     +27 -15    Template2/lib/Template/Stash.pm
  
  Index: Stash.pm
  ===================================================================
  RCS file: /template-toolkit/Template2/lib/Template/Stash.pm,v
  retrieving revision 2.101
  retrieving revision 2.102
  diff -u -r2.101 -r2.102
  --- Stash.pm	2006/05/14 07:12:20	2.101
  +++ Stash.pm	2006/05/25 11:23:35	2.102
  @@ -18,7 +18,7 @@
   #
   #----------------------------------------------------------------------------
   #
  -# $Id: Stash.pm,v 2.101 2006/05/14 07:12:20 abw Exp $
  +# $Id: Stash.pm,v 2.102 2006/05/25 11:23:35 abw Exp $
   #
   #============================================================================
   
  @@ -28,7 +28,7 @@
   
   use strict;
   
  -our $VERSION = sprintf("%d.%02d", q$Revision: 2.101 $ =~ /(\d+)\.(\d+)/);
  +our $VERSION = sprintf("%d.%02d", q$Revision: 2.102 $ =~ /(\d+)\.(\d+)/);
   our $DEBUG   = 0 unless defined $DEBUG;
   our $PRIVATE = qr/^[_.]/;
   
  @@ -83,20 +83,32 @@
           $pattern = '' unless defined $pattern;
           $replace = '' unless defined $replace;
           $global  = 1  unless defined $global;
  -        my $expand = sub {
  -            my ($chunk, $start, $end) = @_;
  -            $chunk =~ s{ \\(\\|\$) | \$ (\d+) }{
  -                $1 ? $1
  -                    : ($2 > $#$start || $2 == 0) ? '' 
  -                    : substr($text, $start->[$2], $end->[$2] - $start->[$2]);
  -            }exg;
  -            $chunk;
  -        };
  -        if ($global) {
  -            $text =~ s{$pattern}{ &$expand($replace, [@-], [@+]) }eg;
  -        } 
  +
  +        if ($replace =~ /\$\d+/) {
  +            # replacement string may contain backrefs
  +            my $expand = sub {
  +                my ($chunk, $start, $end) = @_;
  +                $chunk =~ s{ \\(\\|\$) | \$ (\d+) }{
  +                    $1 ? $1
  +                        : ($2 > $#$start || $2 == 0) ? '' 
  +                        : substr($text, $start->[$2], $end->[$2] - $start->[$2]);
  +                }exg;
  +                $chunk;
  +            };
  +            if ($global) {
  +                $text =~ s{$pattern}{ &$expand($replace, [@-], [@+]) }eg;
  +            } 
  +            else {
  +                $text =~ s{$pattern}{ &$expand($replace, [@-], [@+]) }e;
  +            }
  +        }
           else {
  -            $text =~ s{$pattern}{ &$expand($replace, [@-], [@+]) }e;
  +            if ($global) {
  +                $text =~ s/$pattern/$replace/g;
  +            } 
  +            else {
  +                $text =~ s/$pattern/$replace/;
  +            }
           }
           return $text;
       },