The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Object::Attribute::Cached - cache complex object attributes

SYNOPSIS

        use Object::Attribute::Cached
                attribute1 => sub { shift->some_complex_task },
                squared => sub { shift->{num} ** 2 },
                uptosquare => sub { 1 .. shift->squared },
                squaredsquared => sub { map $_ ** 2, shift->uptosquare };

DESCRIPTION

This provides a simple interface to writing simple caching attribute methods.

It avoids having to write code like:

        sub parsed_query { 
                my $self = shift;
                $self->{_cached_parsed_query} ||= $self->parse_the_query;
                return $self->{_cached_parsed_query};
        }

Instead you can just declare:

        use Object::Attribute::Cached
                parsed_query => sub { shift->parse_the_query };

It's nothing fancy, and it might get confused if you've context-specific code lurking in there as it attempts to cope with these attributes being able to be lists and hashes, but it's Good Enough for most of what I need.

AUTHOR

Tony Bowden, <kasei@tmtm.com>.

COPYRIGHT

Copyright (C) 2003 Kasei. All rights reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.