[Templates] patch for Stash to use objects as arrays

Stephen Howard howars@alum.rpi.edu
Tue, 09 Dec 2003 19:29:44 -0500


I realized that the hash implementation isn't complete for objects 
either.  And in correcting this, i realized that there was a better 
location for the array changes i made in the last patch:

--- /usr/lib/perl5/site_perl/5.6.1/i386-linux/Template/Stash.orig.pm    
Fri Dec  5 17:06:17 2003
+++ /usr/lib/perl5/site_perl/5.6.1/i386-linux/Template/Stash.pm Tue Dec  
9 18:24:43 2003
@@ -613,14 +613,28 @@
            die $@ if ref($@) || ($@ !~ /Can't locate object method/);

            # failed to call object method, so try some fallbacks
-           if (UNIVERSAL::isa($root, 'HASH')
-               && defined($value = $root->{ $item })) {
-               return $value unless ref $value eq 'CODE';          ## 
RETURN
-               @result = &$value(@$args);
+           if (UNIVERSAL::isa($root, 'HASH') ) {
+               if( defined($value = $root->{ $item })) {
+                   return $value unless ref $value eq 'CODE';      ## 
RETURN
+                   @result = &$value(@$args);
+                }
+               elsif ($value = $HASH_OPS->{ $item }) {
+                   @result = &$value($root, @$args);
+                }
            }
-           elsif (UNIVERSAL::isa($root, 'ARRAY')
-                  && ($value = $LIST_OPS->{ $item })) {
-               @result = &$value($root, @$args);
+           elsif (UNIVERSAL::isa($root, 'ARRAY') ) {
+                if( $value = $LIST_OPS->{ $item }) {
+                   @result = &$value($root, @$args);
+                }
+                elsif( $item =~ /^-?\d+$/ ) {
+                   $value = $root->[$item];
+                   return $value unless ref $value eq 'CODE';      ## 
RETURN
+                   @result = &$value(@$args);                      ## 
@result
+                }
+                elsif ( ref $item eq 'ARRAY' ) {
+                    # array slice
+                    return [@$root[@$item]];                        ## 
RETURN
+                }
            }
            elsif ($value = $SCALAR_OPS->{ $item }) {
                @result = &$value($root, @$args);