Here's the kind of thing I ended up using:
#!/usr/bin/perl use warnings; use strict; my $test = Foo->new(); print $test->read_only, "\n"; #$test->read_only("cheese"); # Will error print $test->read_write, "\n"; $test->read_write("cheese"); print $test->read_write, "\n"; package Foo; use vars '$AUTOLOAD'; sub new { bless { read_only => { value => "foo", editable => 0, }, read_write => { value => "bar", editable => 1, } } }; sub AUTOLOAD { no strict "refs"; my ($self, $new_value) = @_; my $field = $AUTOLOAD; $field =~ s/.*:://; die "No such method $AUTOLOAD. Barfing\n" unless exists $self->{$field}; if ($self->{$field}{editable}) { *$AUTOLOAD = sub { my $self = shift; $self->{$field}{value} = shift if @_; $self->{$field}{value}; }; } else { *$AUTOLOAD = sub { my $self = shift; die "Can't modify readonly field '$field'" if +@_; $self->{$field}{value}; }; } goto &$AUTOLOAD; } sub DESTROY { 1; }

davis
It's not easy to juggle a pregnant wife and a troubled child, but somehow I managed to fit in eight hours of TV a day.

In reply to Re: Subroutines with differing behaviour created from AUTOLOAD by davis
in thread Subroutines with differing behaviour created from AUTOLOAD by davis

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.