| 1 | |||
| 2 | package UR::Util; | ||
| 3 | |||
| 4 | use warnings; | ||
| 5 | use strict; | ||
| 6 | require UR; | ||
| 7 | our $VERSION = "0.46"; # UR $VERSION; | ||
| 8 | use Cwd; | ||
| 9 | use Data::Dumper; | ||
| 10 | use Clone::PP; | ||
| 11 | use Config; | ||
| 12 | use Module::Runtime v0.014 qw(module_notional_filename); | ||
| 13 | |||
| 14 | sub on_destroy(&) { | ||
| 15 | my $sub = shift; | ||
| 16 | unless ($sub) { | ||
| 17 | Carp::confess("expected an anonymous sub!") | ||
| 18 | } | ||
| 19 | return bless($sub, "UR::Util::CallOnDestroy"); | ||
| 20 | } | ||
| 21 | |||
| 22 | # used only by the above sub | ||
| 23 | # the local $@ ensures that we this does not stomp on thrown exceptions | ||
| 24 | sub UR::Util::CallOnDestroy::DESTROY { local $@; shift->(); } | ||
| 25 | |||
| 26 | sub d { | ||
| 27 | Data::Dumper->new([@_])->Terse(1)->Indent(0)->Useqq(1)->Dump; | ||
| 28 | } | ||
| 29 | |||
| 30 | sub null_sub { } | ||
| 31 | |||
| 32 | sub used_libs { | ||
| 33 | my @extra; | ||
| 34 | my @compiled_inc = UR::Util::compiled_inc(); | ||
| 35 | my @perl5lib = split(':', $ENV{PERL5LIB}); | ||
| 36 | map { $_ =~ s/\/+$// } (@compiled_inc, @perl5lib); # remove trailing slashes | ||
| 37 | map { $_ = Cwd::abs_path($_) || $_ } (@compiled_inc, @perl5lib); | ||
| 38 | for my $inc (@INC) { | ||
| 39 | $inc =~ s/\/+$//; | ||
| 40 | my $abs_inc = Cwd::abs_path($inc) || $inc; # should already be expanded by UR.pm | ||
| 41 | next if (grep { $_ =~ /^$abs_inc$/ } @compiled_inc); | ||
| 42 | next if (grep { $_ =~ /^$abs_inc$/ } @perl5lib); | ||
| 43 | next if ((File::Spec->splitdir($inc))[-1] eq $Config{archname}); | ||
| 44 | push @extra, $inc; | ||
| 45 | } | ||
| 46 | |||
| 47 | unshift @extra, ($ENV{PERL_USED_ABOVE} ? split(":", $ENV{PERL_USED_ABOVE}) : ()); | ||
| 48 | |||
| 49 | map { $_ =~ s/\/+$// } @extra; # remove trailing slashes again | ||
| 50 | @extra = _unique_elements(@extra); | ||
| 51 | |||
| 52 | return @extra; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub _unique_elements { | ||
| 56 | my @list = @_; | ||
| 57 | my %seen = (); | ||
| 58 | my @unique = grep { ! $seen{$_} ++ } @list; | ||
| 59 | return @unique; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub used_libs_perl5lib_prefix { | ||
| 63 | my $prefix = ""; | ||
| 64 | for my $i (used_libs()) { | ||
| 65 | $prefix .= "$i:"; | ||
| 66 | } | ||
| 67 | return $prefix; | ||
| 68 | } | ||
| 69 | |||
| 70 | sub touch_file { | ||
| 71 | my $filename = shift; | ||
| 72 | open(my $fh, '>>', $filename); | ||
| 73 | } | ||
| 74 | |||
| 75 | my @compiled_inc; | ||
| 76 | BEGIN { | ||
| 77 | use Config; | ||
| 78 | |||
| 79 | my @var_list = ( | ||
| 80 | 'updatesarch', 'updateslib', | ||
| 81 | 'archlib', 'privlib', | ||
| 82 | 'sitearch', 'sitelib', 'sitelib_stem', | ||
| 83 | 'vendorarch', 'vendorlib', 'vendorlib_stem', | ||
| 84 | 'extrasarch', 'extraslib', | ||
| 85 | ); | ||
| 86 | |||
| 87 | for my $var_name (@var_list) { | ||
| 88 | if ($var_name =~ /_stem$/ && $Config{$var_name}) { | ||
| 89 | my @stem_list = (split(' ', $Config{'inc_version_list'}), ''); | ||
| 90 | push @compiled_inc, map { $Config{$var_name} . "/$_" } @stem_list | ||
| 91 | } else { | ||
| 92 | push @compiled_inc, $Config{$var_name} if $Config{$var_name}; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | # UR locks in relative paths when loaded so instead of adding '.' we add cwd | ||
| 97 | push @compiled_inc, Cwd::cwd() if (${^TAINT} == 0); | ||
| 98 | |||
| 99 | map { $_ =~ s/\/+/\//g } @compiled_inc; | ||
| 100 | map { $_ =~ s/\/+$// } @compiled_inc; | ||
| 101 | } | ||
| 102 | sub compiled_inc { | ||
| 103 | return @compiled_inc; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub deep_copy { | ||
| 107 | return Clone::PP::clone($_[0]); | ||
| 108 | } | ||
| 109 | |||
| 110 | sub value_positions_map { | ||
| 111 | my ($array) = @_; | ||
| 112 | my %value_pos; | ||
| 113 | for (my $pos = 0; $pos < @$array; $pos++) { | ||
| 114 | my $value = $array->[$pos]; | ||
| 115 | if (exists $value_pos{$value}) { | ||
| 116 | die "Array has duplicate values, which cannot unambiguously be given value positions!" | ||
| 117 | . Data::Dumper::Dumper($array); | ||
| 118 | } | ||
| 119 | $value_pos{$value} = $pos; | ||
| 120 | } | ||
| 121 | return \%value_pos; | ||
| 122 | } | ||
| 123 | |||
| 124 | sub positions_of_values { | ||
| 125 | # my @pos = positions_of_values(\@unordered_crap, \@correct_order); | ||
| 126 | # my @fixed = @unordered_crap[@pos]; | ||
| 127 | my ($unordered_array,$ordered_array) = @_; | ||
| 128 | my $map = value_positions_map($unordered_array); | ||
| 129 | my @translated_positions; | ||
| 130 | $#translated_positions = $#$ordered_array; | ||
| 131 | for (my $pos = 0; $pos < @$ordered_array; $pos++) { | ||
| 132 | my $value = $ordered_array->[$pos]; | ||
| 133 | my $unordered_position = $map->{$value}; | ||
| 134 | $translated_positions[$pos] = $unordered_position; | ||
| 135 | } | ||
| 136 | # self-test: | ||
| 137 | # my @now_ordered = @$unordered_array[@translated_positions]; | ||
| 138 | # unless ("@now_ordered" eq "@$ordered_array") { | ||
| 139 | # Carp::confess() | ||
| 140 | # } | ||
| 141 | return @translated_positions; | ||
| 142 | } | ||
| 143 | |||
| 144 | |||
| 145 | # Get all combinations of values | ||
| 146 | # input is a list of listrefs of values | ||
| 147 | sub combinations_of_values { | ||
| 148 | return [] unless @_; | ||
| 149 | |||
| 150 | my $first_values = shift; | ||
| 151 | |||
| 152 | $first_values = [ $first_values ] unless (ref($first_values) and ref($first_values) eq 'ARRAY'); | ||
| 153 | |||
| 154 | my @retval; | ||
| 155 | foreach my $sub_combination ( &combinations_of_values(@_) ) { | ||
| 156 | foreach my $value ( @$first_values ) { | ||
| 157 | push @retval, [$value, @$sub_combination]; | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | return @retval; | ||
| 162 | } | ||
| 163 | |||
| 164 | # generate a method | ||
| 165 | sub _define_method { | ||
| 166 | my $class = shift; | ||
| 167 | my (%opts) = @_; | ||
| 168 | |||
| 169 | # create method name | ||
| 170 | my $method = $opts{pkg} . '::' . $opts{property}; | ||
| 171 | |||
| 172 | # determine return value type | ||
| 173 | my $retval; | ||
| 174 | if (defined($opts{value})) | ||
| 175 | { | ||
| 176 | my $refval = ref($opts{value}); | ||
| 177 | $retval = ($refval) ? $refval : 'SCALAR'; | ||
| 178 | } | ||
| 179 | else | ||
| 180 | { | ||
| 181 | $retval = 'SCALAR'; | ||
| 182 | } | ||
| 183 | |||
| 184 | # start defining method | ||
| 185 | my $substr = "sub $method { my \$self = shift; "; | ||
| 186 | |||
| 187 | # set default value | ||
| 188 | $substr .= "\$self->{$opts{property}} = "; | ||
| 189 | my $dd = Data::Dumper->new([ $opts{value} ]); | ||
| 190 | $dd->Terse(1); # do not print ``$VAR1 ='' | ||
| 191 | $substr .= $dd->Dump; | ||
| 192 | $substr .= " unless defined(\$self->{$opts{property}}); "; | ||
| 193 | |||
| 194 | # array or scalar? | ||
| 195 | if ($retval eq 'ARRAY') { | ||
| 196 | if ($opts{access} eq 'rw') { | ||
| 197 | # allow setting of array | ||
| 198 | $substr .= "\$self->{$opts{property}} = [ \@_ ] if (\@_); "; | ||
| 199 | } | ||
| 200 | |||
| 201 | # add return value | ||
| 202 | $substr .= "return \@{ \$self->{$opts{property}} }; "; | ||
| 203 | } | ||
| 204 | else { # scalar | ||
| 205 | if ($opts{access} eq 'rw') { | ||
| 206 | # allow setting of scalar | ||
| 207 | $substr .= "\$self->{$opts{property}} = \$_[0] if (\@_); "; | ||
| 208 | } | ||
| 209 | |||
| 210 | # add return value | ||
| 211 | $substr .= "return \$self->{$opts{property}}; "; | ||
| 212 | } | ||
| 213 | |||
| 214 | # end the subroutine definition | ||
| 215 | $substr .= "}"; | ||
| 216 | |||
| 217 | # actually define the method | ||
| 218 | no warnings qw(redefine); | ||
| 219 | eval($substr); | ||
| 220 | if ($@) { | ||
| 221 | # fatal error since this is like a failed compilation | ||
| 222 | die("failed to defined method $method {$substr}:$@"); | ||
| 223 | } | ||
| 224 | return 1; | ||
| 225 | } | ||
| 226 | |||
| 227 | =pod | ||
| 228 | |||
| 229 | =over | ||
| 230 | |||
| 231 | =item path_relative_to | ||
| 232 | |||
| 233 | $rel_path = UR::Util::path_relative_to($base, $target); | ||
| 234 | |||
| 235 | Returns the pathname to $target relative to $base. If $base | ||
| 236 | and $target are the same, then it returns '.'. If $target is | ||
| 237 | a subdirectory of of $base, then it returns the portion of $target | ||
| 238 | that is unique compared to $base. If $target is not a subdirectory | ||
| 239 | of $base, then it returns a relative pathname starting with $base. | ||
| 240 | |||
| 241 | =back | ||
| 242 | |||
| 243 | =cut | ||
| 244 | |||
| 245 | sub path_relative_to { | ||
| 246 | my($base,$target) = @_; | ||
| 247 | |||
| 248 | $base = Cwd::abs_path($base); | ||
| 249 | $target = Cwd::abs_path($target); | ||
| 250 | |||
| 251 | my @base_path_parts = split('/', $base); | ||
| 252 | my @target_path_parts = split('/', $target); | ||
| 253 | my $i; | ||
| 254 | for ($i = 0; | ||
| 255 | $i < @base_path_parts and $base_path_parts[$i] eq $target_path_parts[$i]; | ||
| 256 | $i++ | ||
| 257 | ) { ; } | ||
| 258 | |||
| 259 | my $rel_path = '../' x (scalar(@base_path_parts) - $i) | ||
| 260 | . | ||
| 261 | join('/', @target_path_parts[$i .. $#target_path_parts]); | ||
| 262 | $rel_path = '.' unless length($rel_path); | ||
| 263 | return $rel_path; | ||
| 264 | } | ||
| 265 | |||
| 266 | =pod | ||
| 267 | |||
| 268 | =over | ||
| 269 | |||
| 270 | =item generate_readwrite_methods | ||
| 271 | |||
| 272 | UR::Util->generate_readwrite_methods | ||
| 273 | ( | ||
| 274 | some_scalar_property => 1, | ||
| 275 | some_array_property => [] | ||
| 276 | ); | ||
| 277 | |||
| 278 | This method generates accessor/set methods named after the keys of its | ||
| 279 | hash argument. The type of function generated depends on the default | ||
| 280 | value provided as the hash key value. If the hash key is a scalar, a | ||
| 281 | scalar method is generated. If the hash key is a reference to an | ||
| 282 | array, an array method is generated. | ||
| 283 | |||
| 284 | This method does not overwrite class methods that already exist. | ||
| 285 | |||
| 286 | =back | ||
| 287 | |||
| 288 | =cut | ||
| 289 | |||
| 290 | sub generate_readwrite_methods | ||
| 291 | { | ||
| 292 | my $class = shift; | ||
| 293 | my %properties = @_; | ||
| 294 | |||
| 295 | # get package of caller | ||
| 296 | my $pkg = caller; | ||
| 297 | |||
| 298 | # loop through properties | ||
| 299 | foreach my $property (keys(%properties)) { | ||
| 300 | # do not overwrite defined methods | ||
| 301 | next if $pkg->can($property); | ||
| 302 | |||
| 303 | # create method | ||
| 304 | $class->_define_method | ||
| 305 | ( | ||
| 306 | pkg => $pkg, | ||
| 307 | property => $property, | ||
| 308 | value => $properties{$property}, | ||
| 309 | access => 'rw' | ||
| 310 | ); | ||
| 311 | } | ||
| 312 | |||
| 313 | return 1; | ||
| 314 | } | ||
| 315 | |||
| 316 | =pod | ||
| 317 | |||
| 318 | =over | ||
| 319 | |||
| 320 | =item generate_readwrite_methods_override | ||
| 321 | |||
| 322 | UR::Util->generate_readwrite_methods_override | ||
| 323 | ( | ||
| 324 | some_scalar_property => 1, | ||
| 325 | some_array_property => [] | ||
| 326 | ); | ||
| 327 | |||
| 328 | Same as generate_readwrite_function except that we force the functions | ||
| 329 | into the namespace even if the function is already defined | ||
| 330 | |||
| 331 | =back | ||
| 332 | |||
| 333 | =cut | ||
| 334 | |||
| 335 | sub generate_readwrite_methods_override | ||
| 336 | { | ||
| 337 | my $class = shift; | ||
| 338 | my %properties = @_; | ||
| 339 | |||
| 340 | # get package of caller | ||
| 341 | my $pkg = caller; | ||
| 342 | |||
| 343 | # generate the methods for each property | ||
| 344 | foreach my $property (keys(%properties)) { | ||
| 345 | # create method | ||
| 346 | $class->_define_method | ||
| 347 | ( | ||
| 348 | pkg => $pkg, | ||
| 349 | property => $property, | ||
| 350 | value => $properties{$property}, | ||
| 351 | access => 'rw' | ||
| 352 | ); | ||
| 353 | } | ||
| 354 | |||
| 355 | return 1; | ||
| 356 | } | ||
| 357 | |||
| 358 | =pod | ||
| 359 | |||
| 360 | =over | ||
| 361 | |||
| 362 | =item generate_readonly_methods | ||
| 363 | |||
| 364 | UR::Util->generate_readonly_methods | ||
| 365 | ( | ||
| 366 | some_scalar_property => 1, | ||
| 367 | some_array_property => [] | ||
| 368 | ); | ||
| 369 | |||
| 370 | This method generates accessor methods named after the keys of its | ||
| 371 | hash argument. The type of function generated depends on the default | ||
| 372 | value provided as the hash key value. If the hash key is a scalar, a | ||
| 373 | scalar method is generated. If the hash key is a reference to an | ||
| 374 | array, an array method is generated. | ||
| 375 | |||
| 376 | This method does not overwrite class methods that already exist. | ||
| 377 | |||
| 378 | =back | ||
| 379 | |||
| 380 | =cut | ||
| 381 | |||
| 382 | sub generate_readonly_methods | ||
| 383 | { | ||
| 384 | my $class = shift; | ||
| 385 | my %properties = @_; | ||
| 386 | |||
| 387 | # get package of caller | ||
| 388 | my ($pkg) = caller; | ||
| 389 | |||
| 390 | # loop through properties | ||
| 391 | foreach my $property (keys(%properties)) { | ||
| 392 | # do no overwrite already defined methods | ||
| 393 | next if $pkg->can($property); | ||
| 394 | |||
| 395 | # create method | ||
| 396 | $class->_define_method | ||
| 397 | ( | ||
| 398 | pkg => $pkg, | ||
| 399 | property => $property, | ||
| 400 | value => $properties{$property}, | ||
| 401 | access => 'ro' | ||
| 402 | ); | ||
| 403 | } | ||
| 404 | |||
| 405 | return 1; | ||
| 406 | } | ||
| 407 | |||
| 408 | =pod | ||
| 409 | |||
| 410 | =over | ||
| 411 | |||
| 412 | =item object | ||
| 413 | |||
| 414 | my $o = UR::Util::object($something); | ||
| 415 | |||
| 416 | Return the object form of the supplied argument. For regular objects, it | ||
| 417 | returns the argument unchanged. For singleton class names, it returns the | ||
| 418 | instance of the Singleton. For other class names, it throws an exception. | ||
| 419 | |||
| 420 | =back | ||
| 421 | |||
| 422 | =cut | ||
| 423 | |||
| 424 | sub object { | ||
| 425 | my $it = shift; | ||
| 426 | |||
| 427 | unless (ref $it) { | ||
| 428 | if ($it->isa('UR::Singleton')) { | ||
| 429 | $it = $it->_singleton_object(); | ||
| 430 | } else { | ||
| 431 | Carp::croak("Expected an object instance or Singleton class name, but got '$it'"); | ||
| 432 | } | ||
| 433 | } | ||
| 434 | return $it; | ||
| 435 | } | ||
| 436 | |||
| 437 | =pod | ||
| 438 | |||
| 439 | =over | ||
| 440 | |||
| 441 | =item mapreduce_grep | ||
| 442 | |||
| 443 | my @matches = UR::Util->map_reduce_grep { shift->some_test } @candidates; | ||
| 444 | |||
| 445 | Works similar to the Perl C<grep> builtin, but in a possibly-parallel fashion. | ||
| 446 | If the environment variable UR_NR_CPU is set to a number greater than one, it | ||
| 447 | will fork off child processes to perform the test on slices of the input | ||
| 448 | list, collect the results, and return the matching items as a list. | ||
| 449 | |||
| 450 | The test function is called with a single argument, an item from the list to | ||
| 451 | be tested, and should return a true of false value. | ||
| 452 | |||
| 453 | =back | ||
| 454 | |||
| 455 | =cut | ||
| 456 | |||
| 457 | sub mapreduce_grep($&@) { | ||
| 458 | my $class = shift; | ||
| 459 | my $subref = shift; | ||
| 460 | #$DB::single = 1; | ||
| 461 | |||
| 462 | |||
| 463 | # First check fast... should we do parallel at all? | ||
| 464 | if (!$ENV{'UR_NR_CPU'} or $ENV{'UR_NR_CPU'} < 2) { | ||
| 465 | #return grep { $subref->($_) } @_; | ||
| 466 | my @ret = grep { $subref->($_) } @_; | ||
| 467 | return @ret; | ||
| 468 | } | ||
| 469 | |||
| 470 | my(@read_handles, @child_pids); | ||
| 471 | my $cleanup = sub { | ||
| 472 | foreach my $handle ( @read_handles ) { | ||
| 473 | $handle->close(); | ||
| 474 | } | ||
| 475 | |||
| 476 | kill 'TERM', @child_pids; | ||
| 477 | |||
| 478 | foreach my $pid ( @child_pids ) { | ||
| 479 | waitpid($pid,0); | ||
| 480 | } | ||
| 481 | }; | ||
| 482 | |||
| 483 | my @things_to_check = @_; | ||
| 484 | my($children, $length,$parent_last); | ||
| 485 | if ($ENV{'UR_NR_CPU'}) { | ||
| 486 | $length = POSIX::ceil(scalar(@things_to_check) / $ENV{'UR_NR_CPU'}); | ||
| 487 | $children = $ENV{'UR_NR_CPU'} - 1; | ||
| 488 | } else { | ||
| 489 | $children = 0; | ||
| 490 | $parent_last = $#things_to_check; | ||
| 491 | } | ||
| 492 | |||
| 493 | # FIXME - There needs to be some code in here to disconnect datasources | ||
| 494 | # Oracle in particular (maybe all DBs?), stops working right unless you | ||
| 495 | # disconnect before forking | ||
| 496 | |||
| 497 | my $start = $length; # First child starts checking after parent's range | ||
| 498 | $parent_last = $length - 1; | ||
| 499 | while ($children-- > 0) { | ||
| 500 | my $pipe = IO::Pipe->new(); | ||
| 501 | unless ($pipe) { | ||
| 502 | Carp::carp("pipe() failed: $!\nUnable to create pipes to communicate with child processes to verify transact+ion, falling back to serial verification"); | ||
| 503 | $cleanup->(); | ||
| 504 | $parent_last = $#things_to_check; | ||
| 505 | last; | ||
| 506 | } | ||
| 507 | |||
| 508 | my $pid = fork(); | ||
| 509 | if ($pid) { | ||
| 510 | $pipe->reader(); | ||
| 511 | push @read_handles, $pipe; | ||
| 512 | $start += $length; | ||
| 513 | |||
| 514 | } elsif (defined $pid) { | ||
| 515 | $pipe->writer(); | ||
| 516 | my $last = $start + $length; | ||
| 517 | $last = $#things_to_check if ($last > $#things_to_check); | ||
| 518 | |||
| 519 | #my @objects = grep { $subref->($_) } @things_to_check[$start .. $last]; | ||
| 520 | my @matching; | ||
| 521 | for (my $i = $start; $i <= $last; $i++) { | ||
| 522 | if ($subref->($things_to_check[$i])) { | ||
| 523 | push @matching, $i; | ||
| 524 | } | ||
| 525 | } | ||
| 526 | # FIXME - when there's a more general framework for passing objects between | ||
| 527 | # processes, use that instead | ||
| 528 | #$pipe->printf("%s\n%s\n",$_->class, $_->id) foreach @objects; | ||
| 529 | $pipe->print("$_\n") foreach @matching; | ||
| 530 | |||
| 531 | |||
| 532 | exit; | ||
| 533 | |||
| 534 | } else { | ||
| 535 | Carp::carp("fork() failed: $!\nUnable to create child processes to ver+ify transaction, falling back to seri+al verification"); | ||
| 536 | $cleanup->(); | ||
| 537 | $parent_last = $#things_to_check; | ||
| 538 | } | ||
| 539 | } | ||
| 540 | my @matches = grep { $subref->($_) } @things_to_check[0 .. $parent_last]; | ||
| 541 | |||
| 542 | foreach my $handle ( @read_handles ) { | ||
| 543 | READ_FROM_CHILD: | ||
| 544 | while(1) { | ||
| 545 | my $match_idx = $handle->getline(); | ||
| 546 | last READ_FROM_CHILD unless $match_idx; | ||
| 547 | chomp $match_idx; | ||
| 548 | |||
| 549 | push @matches, $things_to_check[$match_idx]; | ||
| 550 | #my $match_class = $handle->getline(); | ||
| 551 | #last READ_FROM_CHILD unless $match_class; | ||
| 552 | #chomp($match_class); | ||
| 553 | |||
| 554 | #my $match_id = $handle->getline(); | ||
| 555 | #unless (defined $match_id) { | ||
| 556 | # Carp::carp("Protocol error. Tried to get object ID for class $match_class while verifying transaction"+); | ||
| 557 | # last READ_FROM_CHILD; | ||
| 558 | #} | ||
| 559 | #chomp($match_id); | ||
| 560 | |||
| 561 | #push @objects, $match_class->get($match_id); | ||
| 562 | } | ||
| 563 | $handle->close(); | ||
| 564 | } | ||
| 565 | |||
| 566 | $cleanup->(); | ||
| 567 | |||
| 568 | return @matches; | ||
| 569 | } | ||
| 570 | |||
| 571 | |||
| 572 | # Used in several places when printing out hash-like parameters | ||
| 573 | # to the user, such as in error messages | ||
| 574 | sub display_string_for_params_list { | ||
| 575 | my $class = shift; | ||
| 576 | |||
| 577 | my %params; | ||
| 578 | if (ref($_[0]) =~ 'HASH') { | ||
| 579 | %params = %{$_[0]}; | ||
| 580 | } else { | ||
| 581 | %params = @_; | ||
| 582 | } | ||
| 583 | |||
| 584 | my @strings; | ||
| 585 | foreach my $key ( keys %params ) { | ||
| 586 | my $val = $params{$key}; | ||
| 587 | $val = defined($val) ? "'$val'" : '(undef)'; | ||
| 588 | push @strings, "$key => $val"; | ||
| 589 | } | ||
| 590 | return join(', ', @strings); | ||
| 591 | } | ||
| 592 | |||
| 593 | # why isn't something like this in List::Util? | ||
| 594 | # Return a list of 3 listrefs: | ||
| 595 | # 0: items common to both lists | ||
| 596 | # 1: items in the first list only | ||
| 597 | # 2: items in the second list only | ||
| 598 | sub intersect_lists { | ||
| 599 | my ($m,$n) = @_; | ||
| 600 | my %shared; | ||
| 601 | my %monly; | ||
| 602 | my %nonly; | ||
| 603 | @monly{@$m} = @$m; | ||
| 604 | for my $v (@$n) { | ||
| 605 | if ($monly{$v}) { | ||
| 606 | $shared{$v} = delete $monly{$v}; | ||
| 607 | } | ||
| 608 | else{ | ||
| 609 | $nonly{$v} = $v; | ||
| 610 | } | ||
| 611 | } | ||
| 612 | return ( | ||
| 613 | [ values %shared ], | ||
| 614 | [ values %monly ], | ||
| 615 | [ values %nonly ], | ||
| 616 | ); | ||
| 617 | } | ||
| 618 | |||
| 619 | sub is_valid_property_name { | ||
| 620 | my $property_name = shift; | ||
| 621 | return $property_name =~ m/^[_[:alpha:]][_[:alnum:]]*$/; | ||
| 622 | } | ||
| 623 | |||
| 624 | sub is_valid_class_name { | ||
| 625 | my $class = shift; | ||
| 626 | return $class =~ m/^[[:alpha:]]\w*((::|')\w+)*$/; | ||
| 627 | } | ||
| 628 | |||
| 629 | { | ||
| 630 | my %subclass_suffix_for_builtin_symbolic_operator = ( | ||
| 631 | '=' => "Equals", | ||
| 632 | '<' => "LessThan", | ||
| 633 | '>' => "GreaterThan", | ||
| 634 | '[]' => "In", | ||
| 635 | 'in []' => "In", | ||
| 636 | 'ne' => "NotEquals", | ||
| 637 | '<=' => 'LessOrEqual', | ||
| 638 | '>=' => 'GreaterOrEqual', | ||
| 639 | ); | ||
| 640 | my %subclass_suffix_for_builtin_symbolic_operator_negation = ( | ||
| 641 | '<' => 'GreaterOrEqual', # 'not less than' is the same as GreaterOrEqual | ||
| 642 | '<=' => 'GreaterThan', | ||
| 643 | '>' => 'LessOrEqual', | ||
| 644 | '>=' => 'LessThan', | ||
| 645 | 'ne' => 'Equals', | ||
| 646 | 'false' => 'True', | ||
| 647 | 'true' => 'False', | ||
| 648 | ); | ||
| 649 | |||
| 650 | sub class_suffix_for_operator { | ||
| 651 | my $comparison_operator = shift; | ||
| 652 | my $not = 0; | ||
| 653 | if ($comparison_operator and $comparison_operator =~ m/^(\!|not)\s*(.*)/) { | ||
| 654 | $not = 1; | ||
| 655 | $comparison_operator = $2; | ||
| 656 | } | ||
| 657 | |||
| 658 | if (!defined($comparison_operator) or $comparison_operator eq '') { | ||
| 659 | $comparison_operator = '='; | ||
| 660 | } | ||
| 661 | |||
| 662 | my $suffix; | ||
| 663 | if ($not) { | ||
| 664 | $suffix = $subclass_suffix_for_builtin_symbolic_operator_negation{$comparison_operator}; | ||
| 665 | unless ($suffix) { | ||
| 666 | $suffix = $subclass_suffix_for_builtin_symbolic_operator{$comparison_operator} || ucfirst(lc($comparison_operator)); | ||
| 667 | $suffix = "Not$suffix"; | ||
| 668 | } | ||
| 669 | } else { | ||
| 670 | $suffix = $subclass_suffix_for_builtin_symbolic_operator{$comparison_operator} || ucfirst(lc($comparison_operator)); | ||
| 671 | } | ||
| 672 | return $suffix; | ||
| 673 | } | ||
| 674 | } | ||
| 675 | |||
| 676 | # From DBI::quote() | ||
| 677 | # needed in a few places where we need to quote some SQL but don't | ||
| 678 | # have access to a database handle to call quote() on | ||
| 679 | sub sql_quote { | ||
| 680 | my $str = shift; | ||
| 681 | return "NULL" unless defined $str; | ||
| 682 | $str =~ s/'/''/g; # ISO SQL2 | ||
| 683 | return "'$str'"; | ||
| 684 | } | ||
| 685 | |||
| 686 | # Module::Runtime's use_package_optimistically will not throw an exception if | ||
| 687 | # the package cannot be found or if it fails to compile but will if the package | ||
| 688 | # has upstream exceptions, e.g. a missing dependency. We're a little less | ||
| 689 | # "optimistic" so we check if the package is in %INC so we can report whether | ||
| 690 | # it was believed to be loaded or not. | ||
| 691 | sub use_package_optimistically { | ||
| 692 | my $name = Module::Runtime::use_package_optimistically(shift); | ||
| 693 | my $file = module_notional_filename($name); | ||
| 694 | return $INC{$file}; | ||
| 695 | } | ||
| 696 | |||
| 697 | # return a hashref of subroutine names => coderefs | ||
| 698 | sub coderefs_for_package { | ||
| 699 | my $package = shift; | ||
| 700 | |||
| 701 | my %stash = do { | ||
| 702 | no strict 'refs'; | ||
| 703 | my $stash_name = $package . '::'; | ||
| 704 | %$stash_name; | ||
| 705 | }; | ||
| 706 | |||
| 707 | my %subs; | ||
| 708 | local $@; | ||
| 709 | foreach my $name ( keys %stash ) { | ||
| 710 | my $glob = $stash{$name}; | ||
| 711 | next unless my $coderef = eval { *$glob{CODE} }; # constants are SCALAR refs, not typeglobs | ||
| 712 | $subs{$name} = $coderef; | ||
| 713 | } | ||
| 714 | return \%subs; | ||
| 715 | } | ||
| 716 | |||
| 717 | # Given a key in a hashref, if the value is a scalar, wrap it in an arrayref | ||
| 718 | # used by the class initializer to allow some keys in a class definition to | ||
| 719 | # be specified as simple scalars that are normalized to be an arrayref. | ||
| 720 | # Returns false if the value isn't an arrayref or scalar. | ||
| 721 | sub ensure_arrayref { | ||
| 722 | my($new_class, $key) = @_; | ||
| 723 | |||
| 724 | if ($new_class->{$key}) { | ||
| 725 | if (!ref($new_class->{$key})) { | ||
| 726 | # If it's a plain string, wrap it into an arrayref | ||
| 727 | $new_class->{$key} = [ $new_class->{$key} ]; | ||
| 728 | } elsif (ref($new_class->{$key}) ne 'ARRAY') { | ||
| 729 | my $class_name = $new_class->{class_name}; | ||
| 730 | return 0; | ||
| 731 | } | ||
| 732 | } else { | ||
| 733 | $new_class->{$key} = []; | ||
| 734 | } | ||
| 735 | return 1; | ||
| 736 | } | ||
| 737 | |||
| 738 | |||
| 739 | |||
| 740 | 1; | ||
| 741 | |||
| 742 | =pod | ||
| 743 | |||
| 744 | =head1 NAME | ||
| 745 | |||
| 746 | UR::Util - Collection of utility subroutines and methods | ||
| 747 | |||
| 748 | =head1 DESCRIPTION | ||
| 749 | |||
| 750 | This package contains subroutines and methods used by other parts of the | ||
| 751 | infrastructure. These subs are not likely to be useful to outside code. | ||
| 752 | |||
| 753 | =cut | ||
| 754 |