/usr/share/perl5/Rose/DB/Object/MakeMethods/BigNum.pm is in librose-db-object-perl 1:0.797-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 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 | package Rose::DB::Object::MakeMethods::BigNum;
use strict;
use Carp();
our $VERSION = '0.788'; # move up in the file to make CPAN happy
require Math::BigInt;
BIGNUM_VERSION_CHECK:
{
(my $bignum_version = $Math::BigInt::VERSION) =~ s/_//g;
if($bignum_version >= 1.78)
{
Math::BigInt->import(try => 'GMP');
}
}
use Rose::Object::MakeMethods;
our @ISA = qw(Rose::Object::MakeMethods);
use Rose::DB::Object::Constants
qw(STATE_LOADING MODIFIED_COLUMNS MODIFIED_NP_COLUMNS SET_COLUMNS STATE_IN_DB);
our $Debug = 0;
sub bigint
{
my($class, $name, $args) = @_;
my $key = $args->{'hash_key'} || $name;
my $interface = $args->{'interface'} || 'get_set';
my $default = $args->{'default'};
my $check_in = $args->{'check_in'};
my $min = $args->{'min'};
my $max = $args->{'min'};
my $column_name = $args->{'column'} ? $args->{'column'}->name : $name;
my $undef_overrides_default = $args->{'undef_overrides_default'} || 0;
my $init_method;
if(exists $args->{'with_init'} || exists $args->{'init_method'})
{
$init_method = $args->{'init_method'} || "init_$name";
}
##
## Build code snippets
##
my $qkey = $key;
$qkey =~ s/'/\\'/g;
my $qname = $name;
$qname =~ s/"/\\"/g;
my $col_name_escaped = $column_name;
$col_name_escaped =~ s/'/\\'/g;
my $mod_columns_key = ($args->{'column'} ? $args->{'column'}->nonpersistent : 0) ?
MODIFIED_NP_COLUMNS : MODIFIED_COLUMNS;
my $dont_use_default_code = !$undef_overrides_default ? qq(defined \$self->{'$qkey'}) :
qq(defined \$self->{'$qkey'} || ) .
qq((\$self->{STATE_IN_DB()} && !(\$self->{SET_COLUMNS()}{'$col_name_escaped'} || \$self->{'$mod_columns_key'}{'$col_name_escaped'})) || ) .
qq(\$self->{SET_COLUMNS()}{'$col_name_escaped'} || ) .
qq(\$self->{'$mod_columns_key'}{'$col_name_escaped'});
#
# check_in code
#
my $check_in_code = '';
my %check;
if($check_in)
{
$check_in = [ $check_in ] unless(ref $check_in);
$check_in_code=<<"EOF";
if(defined \$value)
{
my \$found = 0;
foreach my \$check (\@\$check_in)
{
if(\$value == \$check)
{
\$found = 1;
last;
}
}
Carp::croak "Invalid $name: '\$value'" unless(\$found);
}
EOF
}
#
# min/max code
#
my $min_max_code = '';
if($min)
{
unless($min =~ /^-?\d+$/)
{
Carp::croak "Invalid minimum value for bigint column $qname: '$min'";
}
$min_max_code =<<"EOF";
no warnings 'uninitialized';
if(\$value < $min)
{
Carp::croak ref(\$self), ": Value \$value for $qname() is too small. ",
"It must be greater than or equal to $min.";
}
EOF
}
if($max)
{
unless($max =~ /^-?\d+$/)
{
Carp::croak "Invalid maximum value for bigint column $qname: '$max'";
}
$min_max_code =<<"EOF";
no warnings 'uninitialized';
if(\$value < $min)
{
Carp::croak ref(\$self), ": Value \$value for $qname() is too large. ",
"It must be less than or equal to $max.";
}
EOF
}
#
# set code
#
my $set_code = qq(\$self->{'$qkey'} = defined \$value ? Math::BigInt->new(\$value) : undef;);
#
# column modified code
#
my $column_modified_code =
qq(\$self->{'$mod_columns_key'}{'$col_name_escaped'} = 1);
#
# return code
#
my($return_code, $return_code_shift);
if(defined $default)
{
$default = defined $default ? Math::BigInt->new($default) : undef;
$return_code=<<"EOF";
return ($dont_use_default_code) ? \$self->{'$qkey'} :
(scalar($column_modified_code,
\$self->{'$qkey'} = \$default));
EOF
}
elsif(defined $init_method)
{
$return_code=<<"EOF";
return \$self->{'$qkey'} if(defined \$self->{'$qkey'});
$column_modified_code;
my \$init_value = \$self->$init_method();
return \$self->{'$qkey'} = defined \$init_value ? Math::BigInt->new(\$init_value) : undef;
EOF
}
else
{
$return_code = qq(return \$self->{'$qkey'};);
$return_code_shift = qq(return shift->{'$qkey'};);
}
$return_code_shift ||= $return_code;
my %methods;
if($interface eq 'get_set')
{
my $code;
# I can't help myself...
if(defined $default || defined $init_method)
{
$code=<<"EOF";
sub
{
my \$self = shift;
if(\@_)
{
my \$value = shift;
$check_in_code
$min_max_code
$set_code
$column_modified_code unless(\$self->{STATE_LOADING()});
$return_code
}
$return_code
};
EOF
}
else
{
$code=<<"EOF";
sub
{
if(\@_ > 1)
{
my \$self = shift;
my \$value = shift;
$check_in_code
$min_max_code
$column_modified_code unless(\$self->{STATE_LOADING()});
return $set_code
}
$return_code_shift
};
EOF
}
my $error;
TRY:
{
local $@;
$Debug && warn "sub $name = ", $code;
$methods{$name} = eval $code;
$error = $@;
}
if($error)
{
Carp::croak "Error in generated code for method $name - $error\n",
"Code was: $code";
}
}
elsif($interface eq 'get')
{
my $code;
# I can't help myself...
if(defined $default || defined $init_method)
{
$code = qq(sub { my \$self = shift; $return_code };);
}
else
{
$code = qq(sub { shift->{'$qkey'} });
}
my $error;
TRY:
{
local $@;
$Debug && warn "sub $name = ", $code;
$methods{$name} = eval $code;
$error = $@;
}
if($error)
{
Carp::croak "Error in generated code for method $name - $error\n",
"Code was: $code";
}
}
elsif($interface eq 'set')
{
my $arg_check_code =
qq(Carp::croak ref(\$_[0]), ": Missing argument in call to $qname" unless(\@_ > 1););
my $code=<<"EOF";
sub
{
$arg_check_code
my \$self = shift;
my \$value = shift;
$check_in_code
$min_max_code
$set_code
$column_modified_code unless(\$self->{STATE_LOADING()});
$return_code
};
EOF
my $error;
TRY:
{
local $@;
$Debug && warn "sub $name = ", $code;
$methods{$name} = eval $code;
$error = $@;
}
if($error)
{
Carp::croak "Error in generated code for method $name - $error\n",
"Code was: $code";
}
}
else { Carp::croak "Unknown interface: $interface" }
return \%methods;
}
1;
__END__
=head1 NAME
Rose::DB::Object::MakeMethods::BigNum - Create object methods for arbitrary-precision numeric attributes for Rose::DB::Object-derived objects.
=head1 SYNOPSIS
package MyDBObject;
our @ISA = qw(Rose::DB::Object);
use Rose::DB::Object::MakeMethods::BigNum
(
bigint =>
[
count =>
{
with_init => 1,
min => 0,
},
# Important: specify very large integer values as strings
tally => { default => '9223372036854775800' },
],
);
sub init_count { 12345 }
...
$obj = MyDBObject->new(...);
print $obj->count; # 12345
print $obj->tally; # 9223372036854775800
=head1 DESCRIPTION
L<Rose::DB::Object::MakeMethods::BigNum> is a method maker that inherits from L<Rose::Object::MakeMethods>. See the L<Rose::Object::MakeMethods> documentation to learn about the interface. The method types provided by this module are described below.
All method types defined by this module are designed to work with objects that are subclasses of (or otherwise conform to the interface of) L<Rose::DB::Object>. See the L<Rose::DB::Object> documentation for more details.
=head1 METHODS TYPES
=over 4
=item B<bigint>
Create get/set methods for big integer attributes. Values are stored internally and returned as L<Math::BigInt> objects. When specifying very large integer values, use strings to be safe. (See an example in the L<synopsis|/SYNOPSIS> above.)
=over 4
=item Options
=over 4
=item B<check_in ARRAYREF>
A reference to an array of valid values. When setting the attribute, if the new value is not equal to one of the valid values, a fatal error will occur.
=item B<default VALUE>
Determines the default value of the attribute.
=item B<hash_key NAME>
The key inside the hash-based object to use for the storage of this
attribute. Defaults to the name of the method.
=item B<init_method NAME>
The name of the method to call when initializing the value of an undefined attribute. Defaults to the method name with the prefix C<init_> added. This option implies C<with_init>.
=item B<interface NAME>
Choose the interface. The default is C<get_set>.
=item B<max INT>
Get or set the maximum value this attribute is allowed to have.
=item B<min INT>
Get or set the minimum value this attribute is allowed to have.
=item B<with_init BOOL>
Modifies the behavior of the C<get_set> and C<get> interfaces. If the attribute is undefined, the method specified by the C<init_method> option is called and the attribute is set to the return value of that method.
=back
=item Interfaces
=over 4
=item Interfaces
=over 4
=item B<get_set>
Creates a get/set method for a big integer object attribute. When called with an argument, the value of the attribute is set. The current value of the attribute is returned.
=item B<get>
Creates an accessor method for a big integer object attribute that returns the current value of the attribute.
=item B<set>
Creates a mutator method for a big integer object attribute. When called with an argument, the value of the attribute is set. If called with no arguments, a fatal error will occur.
=back
=back
Example:
package MyDBObject;
our @ISA = qw(Rose::DB::Object);
use Rose::DB::Object::MakeMethods::BigNum
(
bigint =>
[
count =>
{
with_init => 1,
min => 0,
},
# Important: specify very large integer values as strings
tally => { default => '9223372036854775800' },
],
);
sub init_count { 12345 }
...
$obj = MyDBObject->new(...);
print $obj->count; # 12345
print $obj->tally; # 9223372036854775800
$obj->count(-1); # Fatal error: minimum value is 0
=back
=back
=head1 AUTHOR
John C. Siracusa (siracusa@gmail.com)
=head1 LICENSE
Copyright (c) 2010 by John C. Siracusa. All rights reserved. This program is
free software; you can redistribute it and/or modify it under the same terms
as Perl itself.
|