Your sample code does not address the problem.

The arguments are those passed to the lvalue sub that define what lvalue is returned by the sub, and therefore what gets modified by the assignment.

In order to validate that part of the data that was modified, you need to know those arguments. So, show me how you can avail the FETCH and STORE methods of the tie of those arguments, so that they can achieve the same validation as is shown in the commented out code in the sub modifySubstring()?

#!/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';

Same problem here. How can I perform the validation of the values assigned to the slice, as shown commented out, through a tie, or traits, or a type definition?

#!/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 +!

Examine what is said, not who speaks.
Silence betokens consent.
Love the truth but pardon error.

In reply to Re^13: Assignable Subroutines by BrowserUk
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.