in reply to Tied array to track last-accessed element
You could be so much lazier.
#!/usr/bin/perl use strict; use warnings; use Tie::Array; package Tie::Array::LastAccessed; use base qw( Tie::StdArray ); my %last_accessed; for my $methname ( qw( STORE FETCH ) ) { my $supername = "SUPER::$methname"; my $glob = do { no strict 'refs'; \*{ $methname } }; *$glob = sub { my $self = shift; my ( $idx ) = @_; $last_accessed{ 0 + $self } = $idx; $self->$supername( @_ ); }; } sub which { my $self = shift; $last_accessed{ 0 + $self }; } sub what { my $self = shift; $self->FETCH( $last_accessed{ 0 + $self } ); } sub DESTROY { my $self = shift; delete $last_accessed{ 0 + self }; $self->SUPER::DESTROY(); } 1;
Makeshifts last the longest.
|
|---|