Class::Easy::Base - base package for classes


Class-Easy documentation Contained in the Class-Easy distribution.

Index


Code Index:

NAME

Top

Class::Easy::Base - base package for classes

ABSTRACT

Top

when you use this package, it makes everything of Class::Easy available for you with OOP sause.

SYNOPSIS

Top

SYNOPSIS

	package My::Class;

	use Class::Easy::Base;

	has x => (is => 'ro');

	has 'y';

	1;

	package main;

	my $c = My::Class->new (x => 1, y => 2);

	$c->x;     # return 1

	$c->x (3); # store 3

BEWARE

Top

THIS PACKAGE PUT HERSELF INTO CALLER CLASS @ISA. IF YOU DON'T WANT SUCH STRANGE BEHAVIOUR, PLEASE USE Class::Easy

METHODS

Top

new

create new object

	my $c = My::Class->new (x => 1, y => 2);

attach_paths

make two accessor methods: lib_path and package_path

list_all_subs

return sub list

for detailed explanation, please see 'list_all_subs_for' function in Class::Easy

set_field_values

set field values by calling accessor methods

	$c->set_field_values (x => 3, y => 4);
	# equivalent calls:
	$c->x (3);
	$c->y (4);

AUTHOR

Top

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

BUGS

Top

Please report any bugs or feature requests to my email address, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Class-Easy. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

Top

ACKNOWLEDGEMENTS

Top

COPYRIGHT & LICENSE

Top


Class-Easy documentation Contained in the Class-Easy distribution.
package Class::Easy::Base;

use Class::Easy::Import;

require Class::Easy;

sub import {
	my $mypkg   = shift;
	my $callpkg = caller;
	
	my %params = @_;
	
	# use warnings
	${^WARNING_BITS} ^= ${^WARNING_BITS} ^ $Class::Easy::Import::WARN;
	
	# use strict, use utf8;
	$^H |= $Class::Easy::Import::H;
	
	# use feature
	$^H{feature_switch} = $^H{feature_say} = $^H{feature_state} = 1;
	
	# probably check for try_to_use is enough
	return
		if defined *{"$callpkg\::try_to_use"}{CODE}
			and Class::Easy::sub_fullname (*{"$callpkg\::try_to_use"}{CODE}) eq 'Class::Easy::__ANON__';
	
	# export subs
	*{"$callpkg\::$_"} = \&{"Class::Easy::$_"} foreach @Class::Easy::EXPORT;
	foreach my $p (keys %Class::Easy::EXPORT_FOREIGN) {
		*{"$callpkg\::$_"} = \&{"$p\::$_"} foreach @{$Class::Easy::EXPORT_FOREIGN{$p}};
	}
	
	push @{"$callpkg\::ISA"}, 'Class::Easy::Base';

}

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

sub set_field_values {
	my $self   = shift;
	my %params = @_;
	
	foreach my $k (keys %params) {
		$self->$k ($params{$k});
	}
}

sub list_all_subs {
	my $class = shift;
	
	$class = ref $class if ref $class;
	
	my $sub_by_type = Class::Easy::list_all_subs_for ($class);
	
	wantarray
		? (
			keys %{$sub_by_type->{method}}, 
			keys %{$sub_by_type->{runtime}},
			map {@{$sub_by_type->{inherited}->{$_}}} keys %{$sub_by_type->{inherited}})
		: $sub_by_type;

}

sub attach_paths {
	my $class = shift;
	
	$class = ref $class if ref $class;
	
	my @pack_chunks = split(/\:\:/, $class);
	
	require File::Spec;
	
	my $FS = 'File::Spec';
	
	my $pack_path = join ('/', @pack_chunks) . '.pm';
	my $pack_inc_path = $INC{$pack_path};

	$pack_path = $FS->canonpath ($pack_path);
	
	my $pack_abs_path = $FS->rel2abs ($FS->canonpath ($pack_inc_path));
	Class::Easy::make_accessor ($class, 'package_path', default => $pack_abs_path);
	
	my $lib_path = substr ($pack_abs_path, 0, rindex ($pack_abs_path, $pack_path));
	Class::Easy::make_accessor ($class, 'lib_path', default => $FS->canonpath ($lib_path));
}

1;

__END__