Monks, Is it possible to retrieve the attribute name that is currently being set in a builder method - similar to what caller() does?

No, it is not possible to get that information. However I suggest you use the approach described below as it will not only reduce code duplication, but also make it that much easier to add similar attributes later on and in subclasses.

package This::That; use Moose; has 'FOO' => (is => 'rw', lazy => 1, builder=> '_build_foo'); has 'BAR' => (is => 'rw', lazy => 1, builder=> '_build_bar'); with qw(SQLConnection); sub _build_foo { (shift)->_build_generic('FOO') } sub _build_bar { (shift)->_build_generic('BAR') } sub _build_generic { my ($self, $attribute_name) = @_; my $sth = $self->dbh->prepare('select $attribute_name from table whe +re x=? and y=?'); $sth->execute(1,2); return $sth->fetchrow_array; }
Note too that this approach also makes it possible to have a custom FOO or BAR builder in a subclass. With your previous version you would have needed to change the builder method pointer and do other hackery that would lead to issues later on.

-stvn

In reply to Re: Retrieving attribute name for use in generic Moose builder method by stvn
in thread Retrieving attribute name for use in generic Moose builder method by ZAmonk

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.