/usr/share/perl5/RoPkg/Object.pm is in libropkg-perl 0.4-1.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | ############################################################################
## Copyright (C) 2005 Subredu Manuel <diablo@iasi.roedu.net>. #
## #
## This program is free software; you can redistribute it and/or modify #
## it under the terms of the GNU General Public License as published by #
## the Free Software Foundation; either version 2 of the License, or #
## (at your option) any later version. #
## #
## This program is distributed in the hope that it will be useful, #
## but WITHOUT ANY WARRANTY; without even the implied warranty of #
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
## GNU General Public License for more details. #
## #
## You should have received a copy of the GNU General Public License #
## along with this program; if not, write to the Free Software #
## Foundation, Inc., 59 Temple Place - Suite 330, Boston, #
## MA 02111-1307,USA. #
############################################################################
package RoPkg::Object;
use strict;
use warnings;
use RoPkg::Exceptions;
use Scalar::Util qw(blessed);
use vars qw($VERSION);
$VERSION = '0.3.2';
sub _inject {
my ($self) = @_;
my $methods;
#This is for backward compatibility
if ( $self->{pf} ) {
#rename pf into methods
$self->{methods} = delete $self->{pf};
}
$methods = $self->{methods};
foreach(keys %{$methods}) {
my $method_name = $_;
no strict 'refs';
if ( $methods->{$method_name} ne '__exclude__'
&& ! ref($self)->can($method_name)
&& $methods->{$method_name} ) {
*{ref($self) . q{::} . $_} = sub {
my ($self, $pval) = @_;
if ( !blessed($self) ) {
OutsideClass->throw('Called outside class instance');
}
if ( defined $pval) {
$self->{$method_name} = $pval;
}
return $self->{$method_name};
};
use strict;
}
}
return $self;
}
sub new {
my ($class, %opt) = @_;
my $self;
$self = bless { %opt }, $class;
return $self;
}
sub methods {
my ($self) = @_;
my @methods;
if (!blessed($self)) {
OutsideClass->throw('Called outside class instance');
}
foreach(sort keys %{ $self->{methods} } ) {
next if ( ! $self->can($_) );
push @methods, $_;
}
return (wantarray ? @methods : scalar @methods);
}
sub key {
my ($self, $kvalue) = @_;
if ( !blessed($self) ) {
OutsideClass->throw('Called outside class instance');
}
foreach(keys %{ $self->{methods} } ) {
return $_ if ( $self->{methods}->{$_} eq $kvalue );
}
return 0;
}
sub chkp {
my ($self, @plist) = @_;
if ( !blessed($self) ) {
OutsideClass->throw('Called outside class instance');
}
foreach(@plist) {
if ( !defined $self->{$_} ) {
Param::Missing->throw($_ . ' not defined');
}
}
return 0;
}
1;
__END__
=head1 NAME
RoPkg::Object
=head1 VERSION
0.3.2
=head1 DESCRIPTION
RoPkg::Object is a general pourpose module, designed for Get/Set objects
on which you don't want to spend your time writing annoying Get/Set methods.
The primary use of the module is to be a base class.
=head1 SYNOPSIS
package RoPkg::Person;
use strict;
use warnings;
use RoPkg::Object;
use base qw(RoPkg::Object);
$pf = {
FirstName => 'A person first name',
LastName => 'A person last name'
};
sub new {
my ($class, %opt) = @_;
my $self;
$self = $class->SUPER::new(%opt);
$self->{methods} = $pf;
$self->_inject();
return $self;
}
1;
tester.pl
#!/usr/bin/perl
use strict;
use warnings;
use English qw(-no_match_vars);
use RoPkg::Person;
sub main {
my $p = new RoPkg::Person();
$p->FirstName('John');
$p->LastName('Doe');
print $p->FirstName,' ',$p->LastName,$RS;
}
main();
=head1 SUBROUTINES/METHODS
All methods (besides new()) raise OutsiteClass exception if called
outside a class instance. Each method, may raise other exceptions. Please
read the documentation of that method for aditional information.
=head2 new()
The class contructor. At this moment the constructor does nothing
(besides bless).
=head2 key($value)
Search into methods list for a entry those value is $value. Returns
the method name or 0 if such a method was not found.
=head2 methods()
In list context this method will return a list of method names. In
scalar context returns just the number of methods. Please note that
only the valid methods are considered (tested with can($method_name)).
=head2 chkp(@plist)
Check the current object if the parameters specified in the list
are defined. If a parameter is not defined a Param::Missing
exception is raised.
=head1 SUBCLASSING
As said before, this module is specially used as a base class for those
objects with many SET/GET methods. How can you use this class in your
project ?
As seen in the SYNOPSIS, when you create the new class, in the class
constructor you call for $self->_inject method, who create (at runtime)
the new methods. The list of methods who are gonna be created is actually
a hash reference. A method can be specified like this:
FirstName => q{-}
This means, that _inject will create a get/set method named FirstName.
There are some key values with special meaning:
=over 2
=item *) __exclude__ - the method with this value will not be created by _inject
=item *) q{} - the method with this value will not be created by _inject
=back
If a existing method is available in the class and is also included in the
list of methods who will be created by _inject, that method will be ignored
by _inject.
Each method created by _inject() has the following code:
sub {
my ($self, $pval) = @_;
if ( !blessed($self) ) {
OutsideClass->throw('Called outside class instance');
}
if ( defined $pval) {
$self->{$method_name} = $pval;
}
return $self->{$method_name};
};
=head1 DEPENDENCIES
RoPkg::Object require perl 5.008 or later and the Scalar::Util
module. To run the tests you also need:
=over 3
=item *) Test::More
=item *) Test::Pod
=item *) Test::Pod::Coverage
=back
=head1 DIAGNOSTICS
This module is subject of tests. To run those tests, unpack
the source and use the following command: make test
=head1 CONFIGURATION AND ENVIRONMENT
This module does not use any configuration file or environment
variables.
=head1 INCOMPATIBILITIES
None known to the author
=head1 BUGS AND LIMITATIONS
No known bugs. If you find one please send a detailed report
to me. Please note that the methods are not automatically created.
One must manual call (inside the child object) the method who
"injects" the new methods.
=head1 PERL CRITIC
This module is perl critic level 1 compliant with 2 exceptions.
=head1 AUTHOR
Subredu Manuel <diablo@iasi.roedu.net>
=head1 LICENSE AND COPYRIGHT
Copyright (C) 2005 Subredu Manuel. All Rights Reserved.
This module is free software; you can redistribute it
and/or modify it under the same terms as Perl itself.
The LICENSE file contains the full text of the license.
=cut
|