Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Traditionally, object properties in Perl are retrieved and set either through methods or by using the object as the reference it is.

To automate the creation of accessor methods, I don't use a module, but a simple closure creating loop like this one:

BEGIN { no strict 'refs'; for my $property (qw/id debug display_time default_text/) { *$property = sub { my ($self, $value) = @_; croak "Too many arguments for $property method" if @_ > 2; $self->{$property} = $value if @_ == 2; return $self->{$property}; } } }

In #perlhelp (efnet), xmath pointed out that methods can be used as lvalues (with the lvalue attribute). It appears attributes work on anonymous subs. That knowledge combined leads to something like:

BEGIN { no strict 'refs'; for my $property (qw/id debug display_time default_text/) { *$property = sub : lvalue { $_[0]->{$property} }; } }

So that means:
OLD SITUATION NEW SITUATION $foo->bar; $foo->bar; $foo->bar(15); $foo->bar = 15; { my $temp = $foo->bar; $temp =~ s/foo/bar/; $foo->bar($temp); } $foo->bar =~ s/foo/bar/

Before I'm going to change my coding style, I'd like to know what the community thinks of it. The lvalue attribute is experimental.

Note that I know this doesn't check the new values. Neither did my old code - I don't really care :) And no, I don't like using the hash reference for my external API.

So, what do you think?

- Yes, I reinvent wheels.
- Spam: Visit eurotraQ.

In reply to $foo->bar = 14; by Juerd

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (2)
    As of 2020-08-08 02:56 GMT
    Find Nodes?
      Voting Booth?
      Which rocket would you take to Mars?

      Results (51 votes). Check out past polls.