#!/usr/bin/perl use strict; use warnings; package UglyTie; use Data::Dumper; sub TIESCALAR { my $class = shift; bless [ @_ ], $class; } sub STORE { warn Dumper @_; } sub FETCH { warn Dumper @_; 1; } package SillyClass; sub new { bless \$_[ 1 ], $_[ 0 ]; } sub modifySubstring: lvalue { my( $self, $start, $length ) = @_; substr( $$self, $start, $length ); ## Validate that part of the string that was modified # die 'Bad value' # unless substr( $$self, $start, $length ) =~ m[^[a-z]+$]; } 1; package main; my $silly = SillyClass->new( 'a teststring' ); $silly->modifySubstring( 3, 3 ) = 'ABC'; #### #!/usr/bin/perl use strict; use warnings; package SillyClass2; sub new { my $class = shift; bless \@_, $class; } sub modifySubset: lvalue { my( $self, $start, $end ) = @_; @{ $self }[ $start .. $end ] ## Validate that all values in the slice being assigned are ## non-zero, even and uniq # my %uniq; # die 'Bad value' # unless $end - $start == grep{ # $_ && !( $_ & 1 ) && not ++$seen{ $_ } #} @{ $self }[ $start .. $end ]; } 1; package main; my $silly = SillyClass2->new( map $_ * 2, 1 .. 50 ); $silly->modifySubset( 15, 35 ) = ( 1 .. 20 ); ## should cause an error!