Class-Easy

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

	Release 0.13
	
	fixed multiple issues with log4perl
	added support for more flags in logging

2011-03-25	Ivan Baktsheev <dot.and.thing@gmail.com>

	Release 0.12
	
	major logger rewrite
	major timer rewrite
	slightly better errors explanation


2010-03-28	Ivan Baktsheev <dot.and.thing@gmail.com>

	Release 0.11
	
	fix for win32 bad paths (such as c:/user/local/perl instead of c:\user\local\perl)
	
2010-01-20	Ivan Baktsheev <dot.and.thing@gmail.com>

	Release 0.09
	
	added ability to easy detection "can't locate" use errors
	added easy interface for accessor default value

2009-11-26	Ivan Baktsheev <dot.and.thing@gmail.com>

	Release 0.08
	
	changed test for bad testrs who doesn't have a proper system timer

2009-11-25	Ivan Baktsheev <dot.and.thing@gmail.com>

	Release 0.07
	
	try_to_use_inc
	some documentation
	minor fixes

2009-11-05	Ivan Baktsheev <dot.and.thing@gmail.com>

MANIFEST  view on Meta::CPAN

lib/Class/Easy/Log.pm
lib/Class/Easy/Log/Tie.pm
Easy.xs
Import.pm.PL
Makefile.PL
MANIFEST
META.yml			Module meta-data (added by MakeMaker)
ppport.h
README
t/000-accessor.t
t/001-timer.t
t/002-log.t
t/004-strict-warnings.t
t/005-pod-coverage.t
t/006-base.t
META.json                                Module JSON meta-data (added by MakeMaker)

lib/Class/Easy.pm  view on Meta::CPAN


}

require Class::Easy::Timer;

sub stash_name   ($) { (get_coderef_info($_[0]))[0] }
sub sub_name     ($) { (get_coderef_info($_[0]))[1] }
sub sub_fullname ($) { join '::', get_coderef_info($_[0]) }


our @EXPORT = qw(has try_to_use try_to_use_quiet try_to_use_inc try_to_use_inc_quiet make_accessor timer);
our @EXPORT_OK = qw(sub_name stash_name sub_fullname get_coderef_info);

our %EXPORT_FOREIGN = (
	'Class::Easy::Log' => [qw(debug critical debug_depth logger catch_stderr release_stderr)],
#	'Class::Easy::Timer' => [qw(timer)],
);

our $LOG = '';

sub timer {
	return Class::Easy::Timer->new (@_);
}

sub import {
	my $mypkg   = shift;
	my $callpkg = caller;
	
	my %params = @_;
	
	# use warnings

lib/Class/Easy.pm  view on Meta::CPAN

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.

=head1 SYNOPSIS

SYNOPSIS

	# 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

lib/Class/Easy.pm  view on Meta::CPAN

		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

=head1 FUNCTIONS

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

create accessor named $name in current scope

=cut

lib/Class/Easy.pm  view on Meta::CPAN


=cut

=head2 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.

=cut

=head2 timer

create new L<Class::Easy::Timer> object

=cut

=head2 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'

=cut

=head2 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,

lib/Class/Easy.pm  view on Meta::CPAN

		'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'
			}

lib/Class/Easy/Log.pm  view on Meta::CPAN

			$int_loggers->{$category} = $self;
			
			Class::Easy::make_accessor ((caller)[0], 'log_'.$category, default => sub {
				my $caller1  = [caller (1)];
				my $caller0  = [caller];

				unshift @_, $category, $self, $caller1, $caller0;
				goto &_wrapper;
			});

			Class::Easy::make_accessor ((caller)[0], 'timer_'.$category, default => sub {
				Class::Easy::Timer->new (@_, $self)
			});
		}

	} elsif (defined $driver_config->{$driver_id}) { # driver defined
		my $driver = $driver_config->{$driver_id};
		$self = $driver->{package}->can ($driver->{constructor})->($driver->{package}, $category);

		Class::Easy::make_accessor ((caller)[0], 'log_'.$category, default => sub {
			goto &{$self->can ($driver->{log})};

lib/Class/Easy/Log.pm  view on Meta::CPAN

		pid      => $$,
		category => $self->{category},
		newline  => "\n",
		ts_start => $time - $^T,
		hostname => $hostname, # doesn't reflect hostname changes in runtime
		date     => $time,
		@_
	};

#	TODO: make sure all these values supported
#	R => 'ts_log',   # use timer_${logger} instead
#	C => 'package',  # useless, because we have %M = method
#	F => 'file',     # who cares about script files?
#	l => 'where',    # wtf?
#	p => 'priority', # log level, if written not for robots
#	T => 'stack',    # everything loves java stacks
#	TODO: add date formatting support
	
#	use Data::Dumper;
#	warn Dumper $self->{_layout_fields};
#	warn Dumper [map {$values->{$_}} @{$self->{_layout_fields}}];

lib/Class/Easy/Timer.pm  view on Meta::CPAN

	);
	
	return $interval;
}


1;

=head1 NAME

Class::Easy::Timer - really easy timer

=head1 ABSTRACT

=head1 SYNOPSIS

SYNOPSIS

	use Class::Easy;
	
	# timer doesn't run without properly configured logger
	logger ('default')->appender (*STDERR);

	$t = timer ('sleep one second');

	sleep (1);

	my $interval = $t->lap ('one more second'); # $interval == 1

	warn "your system have bad timer: 1s = ${interval}s"
		if $interval < 1;

	sleep (1);

	$interval = $t->end; # $interval == 1

	warn "your system have bad timer: 1s = ${interval}s"
		if $interval < 1;

	$interval = $t->total; # $interval == 2
	

=head1 METHODS

=head2 new

create timer, start new lap and return timer object

=cut

=head2 lap

get lap duration and start a new lap

=cut

=head2 end

get duration for last lap

=cut

=head2 total

get duration between timer creation and end call

=cut

=head1 AUTHOR

Ivan Baktsheev, C<< <apla at the-singlers.us> >>

=head1 BUGS

Please report any bugs or feature requests to my email address,

t/001-timer.t  view on Meta::CPAN

#!/usr/bin/perl

use Class::Easy;

use Test::More qw(no_plan);

use Data::Dumper;

# without Class::Easy::DEBUG
my $t = timer ('sleep one second');

ok ! defined $t->[0];

ok 0 == $t->lap ('one more second');

ok 0 == $t->end;

ok 0 == $t->total;

# with Class::Easy::DEBUG
logger ('default')->appender (undef);

$t = timer ('sleep one second');

ok $t->[0] eq 'sleep one second';

sleep (1);

my $interval = $t->lap ('one more second');

ok $interval > 0;

warn "your system have bad timer: 1s = ${interval}s"
	if $interval < 1;

sleep (1);

$interval = $t->end;

ok $interval > 0;

warn "your system have bad timer: 1s = ${interval}s"
	if $interval < 1;

$interval = $t->total;

ok $interval > 0;

warn "your system have bad timer: 1s = ${interval}s"
	if $interval < 2;


1;

t/002-log.t  view on Meta::CPAN

ok $logger->appender (*STDERR);
logger ('test');

ok logger (test => *STDERR), 'simplified syntax';

ok log_test ('bbb');
ok $str2 =~ /bbb/;

$str2 = '';

# timer test
my $t = timer_test ('xxx');

sleep (1);

my $interval = $t->end;

ok $interval > 0;

warn "your system have bad timer: 1s = ${interval}s"
	if $interval < 1;

ok $str2 =~ /xxx/;

ok $t = timer ('zzz'), 'default timer belongs to debug category';

sleep (1);

ok ! $t->end;

ok $logger->appender ();

release_stderr;

# TODO: test coderef



( run in 0.803 second using v1.01-cache-2.11-cpan-49f99fa48dc )