File: Interp.pm

package info (click to toggle)
libmason-perl 2.24-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 748 kB
  • sloc: perl: 4,882; makefile: 7
file content (30 lines) | stat: -rw-r--r-- 787 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package Mason::Plugin::LvalueAttributes::Interp;
$Mason::Plugin::LvalueAttributes::Interp::VERSION = '2.24';
use Mason::PluginRole;

after 'modify_loaded_class' => sub {
    my ( $self, $compc ) = @_;
    $self->_add_lvalue_attribute_methods($compc);
};

sub _add_lvalue_attribute_methods {
    my ( $self, $class ) = @_;

    my @attrs = $class->meta->get_all_attributes();
    foreach my $attr (@attrs) {
        if ( $attr->_is_metadata eq 'rw' ) {
            my $name = $attr->name;
            $class->meta->add_method(
                $name,
                sub : lvalue {
                    if ( defined( $_[1] ) ) {
                        $_[0]->{$name} = $_[1];
                    }
                    $_[0]->{$name};
                }
            );
        }
    }
}

1;