| 1 | package JAF; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | our %IMPORTED = (); | ||
| 5 | |||
| 6 | sub import { | ||
| 7 | my $cl = 0; | ||
| 8 | my ($filename, $sub); | ||
| 9 | do { | ||
| 10 | ($filename, $sub) = (caller($cl))[1,3]; | ||
| 11 | $cl++; | ||
| 12 | } while ( $filename && $sub ne '(eval)'); | ||
| 13 | |||
| 14 | if ($filename) { | ||
| 15 | return if exists $IMPORTED{$filename}; | ||
| 16 | $filename =~ s/\.pm$/\//; | ||
| 17 | use DirHandle; | ||
| 18 | if (my $dh = DirHandle->new( $filename )) { | ||
| 19 | foreach my $file ($dh->read) { | ||
| 20 | next if $file !~ /\.pm$/; | ||
| 21 | require "$filename$file"; | ||
| 22 | } | ||
| 23 | } | ||
| 24 | |||
| 25 | $IMPORTED{$filename} = 1; | ||
| 26 | } | ||
| 27 | } | ||
| 28 | |||
| 29 | # set error | ||
| 30 | ################################################################################ | ||
| 31 | sub error () { | ||
| 32 | my ($self, $text) = @_; | ||
| 33 | |||
| 34 | push @{$self->{messages}}, ['error', $text] if $text ne ''; | ||
| 35 | } | ||
| 36 | |||
| 37 | # set message | ||
| 38 | ################################################################################ | ||
| 39 | sub message () { | ||
| 40 | my ($self, $text) = @_; | ||
| 41 | |||
| 42 | push @{$self->{messages}}, ['message', $text] if $text ne ''; | ||
| 43 | } | ||
| 44 | |||
| 45 | # get messages | ||
| 46 | ################################################################################ | ||
| 47 | sub messages () { | ||
| 48 | my $self = shift(); | ||
| 49 | |||
| 50 | my %hash; | ||
| 51 | foreach (@{$self->{messages}}) { | ||
| 52 | $hash{"$_->[0]:$_->[1]"}++; | ||
| 53 | } | ||
| 54 | my $new = [map {[(split ':', $_, 2), $hash{$_}]} keys %hash]; | ||
| 55 | |||
| 56 | $self->{messages} = []; | ||
| 57 | |||
| 58 | return @$new ? $new : undef; | ||
| 59 | } | ||
| 60 | |||
| 61 | ################################################################################ | ||
| 62 | sub AUTOLOAD { | ||
| 63 | no strict 'refs'; | ||
| 64 | my $self = shift; | ||
| 65 | my $module = our $AUTOLOAD; | ||
| 66 | $module =~ s/.*:://; | ||
| 67 | return if $module eq 'DESTROY'; | ||
| 68 | |||
| 69 | my $pkg = ref($self) . '::' . $module; | ||
| 70 | $self->{$module} ||= "$pkg"->new({ parent => $self, dbh => $self->{dbh} }); | ||
| 71 | return $self->{$module}; | ||
| 72 | } | ||
| 73 | |||
| 74 | 1; |