| 1 | #!/usr/bin/perl -w | ||
| 2 | # | ||
| 3 | # Data::Toolkit | ||
| 4 | # | ||
| 5 | # Andrew Findlay | ||
| 6 | # Jan 2008 | ||
| 7 | # andrew.findlay@skills-1st.co.uk | ||
| 8 | # | ||
| 9 | # $Id: Toolkit.pm 388 2013-08-30 15:19:23Z remotesvn $ | ||
| 10 | |||
| 11 | package Data::Toolkit; | ||
| 12 | |||
| 13 | use strict; | ||
| 14 | use Data::Dumper; | ||
| 15 | use Carp; | ||
| 16 | use Clone qw(clone); | ||
| 17 | |||
| 18 | =head1 NAME | ||
| 19 | |||
| 20 | Data::Toolkit | ||
| 21 | |||
| 22 | =head1 DESCRIPTION | ||
| 23 | |||
| 24 | Toolkit for pumping data from one source to another, | ||
| 25 | directory synchronisation etc. | ||
| 26 | |||
| 27 | =head1 SYNOPSIS | ||
| 28 | |||
| 29 | |||
| 30 | =head1 DEPENDENCIES | ||
| 31 | |||
| 32 | Carp | ||
| 33 | Clone | ||
| 34 | Data::Dumper | ||
| 35 | |||
| 36 | =cut | ||
| 37 | |||
| 38 | ######################################################################## | ||
| 39 | # Package globals | ||
| 40 | ######################################################################## | ||
| 41 | |||
| 42 | use vars qw($VERSION); | ||
| 43 | $VERSION = '1.1'; | ||
| 44 | |||
| 45 | # Set this non-zero for debug logging | ||
| 46 | # | ||
| 47 | my $debug = 0; | ||
| 48 | |||
| 49 | ######################################################################## | ||
| 50 | # Constructors and destructors | ||
| 51 | ######################################################################## | ||
| 52 | |||
| 53 | =head1 Constructor | ||
| 54 | |||
| 55 | =head2 new | ||
| 56 | |||
| 57 | my $map = Data::Toolkit->new(); | ||
| 58 | |||
| 59 | Creates an object of type Data::Toolkit | ||
| 60 | |||
| 61 | =cut | ||
| 62 | |||
| 63 | sub new { | ||
| 64 | my $class = shift; | ||
| 65 | my $configParam = shift; | ||
| 66 | |||
| 67 | my $self = {}; | ||
| 68 | |||
| 69 | # Take a copy of the config hash if we were given one | ||
| 70 | # - we don't want to store a ref to the one we were given | ||
| 71 | # in case it is part of another object | ||
| 72 | # | ||
| 73 | if (defined($configParam)) { | ||
| 74 | if ((ref $configParam) ne 'HASH') { | ||
| 75 | croak "Data::Toolkit->new expects a hash ref but was given something else" | ||
| 76 | } | ||
| 77 | |||
| 78 | $self->{config} = clone($configParam); | ||
| 79 | } | ||
| 80 | else { | ||
| 81 | # Start with empty config | ||
| 82 | $self->{config} = {}; | ||
| 83 | } | ||
| 84 | |||
| 85 | |||
| 86 | bless ($self, $class); | ||
| 87 | |||
| 88 | carp "Data::Toolkit->new $self" if $debug; | ||
| 89 | return $self; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub DESTROY { | ||
| 93 | my $self = shift; | ||
| 94 | carp "Data::Toolkit Destroying $self" if $debug; | ||
| 95 | } | ||
| 96 | |||
| 97 | ######################################################################## | ||
| 98 | # Methods | ||
| 99 | ######################################################################## | ||
| 100 | |||
| 101 | =head1 Methods | ||
| 102 | |||
| 103 | =cut | ||
| 104 | |||
| 105 | |||
| 106 | ######################################################################## | ||
| 107 | # Debugging methods | ||
| 108 | ######################################################################## | ||
| 109 | |||
| 110 | =head1 Debugging methods | ||
| 111 | |||
| 112 | =head2 debug | ||
| 113 | |||
| 114 | Set and/or get the debug level for Data::Toolkit | ||
| 115 | |||
| 116 | my $currentDebugLevel = Data::Toolkit->debug(); | ||
| 117 | my $newDebugLevel = Data::Toolkit->debug(1); | ||
| 118 | |||
| 119 | Any non-zero debug level causes the module to print copious debugging information. | ||
| 120 | |||
| 121 | Note that this is a package method, not an object method. It should always be | ||
| 122 | called exactly as shown above. | ||
| 123 | |||
| 124 | All debug information is reported using "carp" from the Carp module, so if | ||
| 125 | you want a full stack backtrace included you can run your program like this: | ||
| 126 | |||
| 127 | perl -MCarp=verbose myProg | ||
| 128 | |||
| 129 | =cut | ||
| 130 | |||
| 131 | # Class method to set and/or get debug level | ||
| 132 | # | ||
| 133 | sub debug { | ||
| 134 | my $class = shift; | ||
| 135 | if (ref $class) { croak "Class method 'debug' called as object method" } | ||
| 136 | # print "DEBUG: ", (join '/', @_), "\n"; | ||
| 137 | $debug = shift if (@_ == 1); | ||
| 138 | return $debug | ||
| 139 | } | ||
| 140 | |||
| 141 | |||
| 142 | ######################################################################## | ||
| 143 | ######################################################################## | ||
| 144 | |||
| 145 | =head1 Author | ||
| 146 | |||
| 147 | Andrew Findlay | ||
| 148 | |||
| 149 | Skills 1st Ltd | ||
| 150 | |||
| 151 | andrew.findlay@skills-1st.co.uk | ||
| 152 | |||
| 153 | http://www.skills-1st.co.uk/ | ||
| 154 | |||
| 155 | =cut | ||
| 156 | |||
| 157 | ######################################################################## | ||
| 158 | ######################################################################## | ||
| 159 | 1; |