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


Class::Easy - make class routine easy


This module is a functionality compilation of some good modules from CPAN. Ideas are taken from Class::Data::Inheritable, Class::Accessor, Modern::Perl and Moose at least.

Instead of building monstrous alternatives to Moose or making thousand modules for every function I need, I decide to write small and efficient libraries for everyday use. Class::Easy::Base is a base component for classes.



        # automatic loading of strict, warnings and utf8, like common::sense
        use Class::Easy::Import;
        # or same as above + functions like 'has', 'try_to_use', 'timer' and 'logger'
        use Class::Easy;
        # try to load package IO::Easy, return 1 when success
        try_to_use ('IO::Easy');
        # try to load package IO::Easy, but search for package existence
        # within %INC instead of symbolic table
        try_to_use_inc ('IO::Easy');
        # for current package
        has "property_ro"; # make readonly object accessor
        has "property_rw", is => 'rw'; # make readwrite object accessor
        has global25 => 25; # make readonly static accessor with value 25
        has "global", global => 1, is => 'rw'; # make readwrite static accessor

        # make subroutine in package main
        make_accessor ('main', 'initialize', default => sub {
                $::initialized = 1;
                return "initialized!";
        # see documentation for Class::Easy::Log
        # string "[PID] [PACKAGE(STRING)] [DBG] something" logged
        debug "something";

        # see documentation for Class::Easy::Timer

        my $t = timer ('long operation');
        # … long operation

        my $time = $t->lap ('another long op');
        # …

        $time = $t->end;
        # $time contains time between last 'lap' or 'timer'
        # and 'end' call

        $time = $t->total;
        # now $time contains total time between timer init
        # and end call


has ($name [, is => 'ro' | 'rw'] [, default => $default], [, global => 1])

create accessor named $name in current scope

make_accessor ($scope, $name)

create accessor in selected scope

try_to_use, try_to_use_quiet

tries to use specified package with printing error message to STDERR or "_quiet" version.

return true value in case of successful operation or existing non-package references in symbol table. correctly works with virtual packages.

takes package name or package name chunks, for example:

        try_to_use ('IO::Easy');
        # or equivalent
        try_to_use (qw(IO Easy));

if you want to separate io errors from syntax errors you may want to check $! variable;

for example:

        use Errno qw(:POSIX);
        if (!try_to_use ('IO::Easy')) {
                die 'file not found for package IO::Easy'
                        if $!{ENOENT};

try_to_use_inc, try_to_use_inc_quiet

similar to the try_to_use, but check for module presence in %INC instead of symbol table lookup.


create new Class::Easy::Timer object

get_coderef_info, stash_name, sub_name, sub_fullname

retrieve real name for coderef. useful for anonymous or imported functions

        get_coderef_info (*{Class::Easy::timer}{CODE}); # ('Class::Easy', 'timer')
        stash_name (*{Class::Easy::timer}{CODE}); # 'Class::Easy'
        sub_name (*{Class::Easy::timer}{CODE}); # 'timer'
        sub_fullname (*{Class::Easy::timer}{CODE}); # 'Class::Easy::timer'

list_all_subs_for, list_local_subs_for

in scalar context return hashref with complete coderef info for class. - key 'inherited' contains all inherited methods, separated by class name, - key 'runtime' contains all code references in current package which point to anonymous method, - key 'method' contains all local methods, - key 'imported' contains all imported subs, separated by class name

                'inherited' => {
                        'My::Circle' => [
                'runtime' => {
                        'global_ro' => 1,
                        'global_one' => 1,
                        'global_one_defined' => 1,
                        'dim_z' => 1,
                        'accessor' => 1
                'method' => {
                        'sub_z' => 1
                'imported' => {
                        'Class::Easy' => {
                                'make_accessor' => 'make_accessor',
                                'try_to_use' => 'try_to_use',
                                'try_to_use_inc' => 'try_to_use_inc',
                                'try_to_use_quiet' => 'try_to_use_quiet',
                                'has' => 'has',
                                'timer' => 'timer',
                                'try_to_use_inc_quiet' => 'try_to_use_inc_quiet'
                        'Class::Easy::Log' => {
                                'critical' => 'critical',
                                'release_stderr' => 'release_stderr',
                                'catch_stderr' => 'catch_stderr',
                                'debug' => 'debug',
                                'debug_depth' => 'debug_depth',
                                'logger' => 'logger'

'local' version of subroutine doesn't contains any inherited methods


Ivan Baktsheev, <apla at>


Please report any bugs or feature requests to my email address, or through the web interface at I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.




Copyright 2008-2009 Ivan Baktsheev

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

1 POD Error

The following errors were encountered while parsing the POD:

Around line 330:

Non-ASCII character seen before =encoding in '…'. Assuming UTF-8