I feel silly having to demonstrate such trivial solutions to someone who otherwise seems quite knowledgable, but here goes:

#!/usr/bin/perl use strict; use warnings; package Tie::EnsureLowercaseSubstr; use Carp; sub TIESCALAR { my $class = shift; my %self; @self{ qw( strref offs len ) } = @_; bless \%self, $class; } sub STORE { my $self = shift; my ( $value ) = @_; croak 'Bad value' unless $value =~ /^[a-z]+$/; substr( ${ $self->{ strref } }, $self->{ offs }, $self->{ len }, $ +value ); } sub FETCH { my $self = shift; substr( ${ $self->{ strref } }, $self->{ offs }, $self->{ len } ); } package SillyClass; sub new { my $class = shift; my ( $value ) = @_; bless \$value, $class; } sub modifySubstring: lvalue { my( $self, $start, $length ) = @_; tie my $response, 'Tie::EnsureLowercaseSubstr', \$$self, $start, $ +length; $response; } package main; my $silly = SillyClass->new( 'a teststring' ); $silly->modifySubstring( 3, 3 ) = 'ABC';

Part two:

#!/usr/bin/perl use strict; use warnings; package Tie::EvenUniqueTrueArray; use Tie::Array; use base qw( Tie::StdArray ); use Carp; my( %Array, %Start, %End ); sub TIEARRAY { my $class = shift; my ( $array, $start, $end ) = @_; my $self = [ @{ $array }[ $start .. $end ] ]; ( $Array{ 0 + $self }, $Start{ 0 + $self }, $End{ 0 + $self } ) = +( $array, $start, $end ); bless $self, $class; } sub STORE { my $self = shift; my ( $idx, $value ) = @_; $self->[ $idx ] = $value; my %seen; croak 'Bad value' if !$value or ( $value & 1 ) or grep $seen{ $_ }++, @$self; } sub DESTROY { my $self = shift; @{ $Array{ 0 + $self } }[ $Start{ 0 + $self } .. $End{ 0 + $self } + ] = @$self; delete $_->{ 0 + $self } for \( %Array, %Start, %End ); } package SillyClass2; sub new { my $class = shift; bless \@_, $class; } sub modifySubset: lvalue { my( $self, $start, $end ) = @_; tie my @response, 'Tie::EvenUniqueTrueArray', \@$self, $start, $en +d; @response; } package main; my $silly = SillyClass2->new( map $_ * 2, 1 .. 50 ); ( $silly->modifySubset( 15, 16 ) ) = ( 20, 22 ); ( $silly->modifySubset( 15, 35 ) ) = ( 1 .. 20 );

All of that was all there in my previous reply.

Goes to show why it's nice that Perl6 will only require a fraction of the red tape, though.

Makeshifts last the longest.


In reply to Re^14: Assignable Subroutines by Aristotle
in thread Assignable Subroutines by dragonchild

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.