Update: Problem solved. Thanks, BrowserUk.


Fellow monks,

Below is code for automatically generating accessor methods with an lvalue attribute.

According to the docs, attributes in general are not available for anonymous subs, which means that the common approach of generating methods by dynamically aliasing closures can't be used if one wants these methods to have an lvalue attribute.

The only alternative I can think of is eval'ing dynamically generated strings. I'm not crazy about this, but a prototype I threw together works well enough, so I thought I'd run it by you folks, in case there are pitfalls I have overlooked. Below is the code along with a brief demo. Your comments would be much appreciated.


# Accessor_Factory.pm use strict; use warnings; package Accessor_Factory; 1; # appease the wrath of require sub make_getsets { my @fields = @_; my $class = caller; for my $field ( @fields ) { my $code = <<EOCODE; { package $class; use Carp 'carp'; sub $field : lvalue { my \$self = shift; carp "Only first argument to method $field of class $class is +used" if \@_ > 1; \$self->{ $field } = shift if \@_; \$self->{ $field }; } } 1; EOCODE eval $code or die $@; } return; } __END__

# Foo.pm use strict; use warnings; package Foo; 1; BEGIN { use Accessor_Factory; Accessor_Factory::make_getsets( qw( bar baz ) ); } sub new { return bless +{}, shift; } __END__

# demo.pl use strict; use warnings; use Foo; my $foo = Foo->new(); $foo->bar = 1; print $foo->bar, "\n"; $foo->bar += 2; print $foo->bar, "\n"; $foo->bar( 5 ); print $foo->bar, "\n"; print $foo->baz = 8, "\n"; print $foo->baz *= 11, "\n"; print $foo->baz( 100 ), "\n"; __END__

1 3 5 8 88 100

Update: Added a few more cases to the demo.

the lowliest monk


In reply to RFC: Autogenerating lvalue methods by tlm

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.