Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ap/object-tiny-lvalue

minimal class builder with lvalue accessors
https://github.com/ap/object-tiny-lvalue

object-oriented-programming oop perl

Last synced: 10 days ago
JSON representation

minimal class builder with lvalue accessors

Awesome Lists containing this project

README

        

use 5.006; use strict; use warnings;

package Object::Tiny::Lvalue;

our $VERSION = '1.084';

sub import {
return unless shift eq __PACKAGE__;
my $pkg = caller;
eval join "\n", (
"package $pkg;",
'our @ISA = "Object::Tiny::Lvalue" unless @ISA;',
map {
defined and /\A[^\W\d]\w*\z/ or die "Invalid accessor name '$_'";
"sub $_ : lvalue { \$_[0]->{$_} }";
} @_
);
die "Failed to generate $pkg" if $@;
return 1;
}

sub new {
my $class = shift;
bless { @_ }, $class;
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Object::Tiny::Lvalue - minimal class builder with lvalue accessors

=head1 SYNOPSIS

Define a class:

package Foo;
use Object::Tiny::Lvalue qw( bar baz );
1;

Use the class:

my $object = Foo->new( bar => 1 );
printf "bar is %s\n", $object->bar;
$object->bar = 2;
printf "bar is now %s\n", $object->bar;

=head1 DESCRIPTION

This is a clone of L, but adjusted to create accessors that return lvalues.

You probably want to use L instead.

=cut