| 1 | package FindApp::Utils::Package::Object; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use v5.10; | ||
| 6 | |||
| 7 | use FindApp::Utils::Carp; | ||
| 8 | use FindApp::Utils::Assert qw(:all); | ||
| 9 | use FindApp::Utils::Syntax qw(:all); | ||
| 10 | use List::Util qw(max); | ||
| 11 | use Scalar::Util qw( | ||
| 12 | blessed | ||
| 13 | looks_like_number | ||
| 14 | reftype | ||
| 15 | ); | ||
| 16 | |||
| 17 | sub selfhere { | ||
| 18 | my $self = shift; | ||
| 19 | my $here = shift || 1; | ||
| 20 | looks_like_number($here) || subcroak_N(2, "'$here' does not look like a number"); | ||
| 21 | $here = ($here <=> @$self) * @$self if abs($here) > @$self; # limit magnitude, keep sign | ||
| 22 | return ($self, $here); | ||
| 23 | } | ||
| 24 | |||
| 25 | use namespace::clean; | ||
| 26 | |||
| 27 | sub PACKAGE ( ;$ ) ; | ||
| 28 | sub UNPACKAGE ( _ ) ; | ||
| 29 | |||
| 30 | use Exporter "import"; | ||
| 31 | our @EXPORT = <{,UN}PACKAGE>; | ||
| 32 | our @EXPORT_OK = @EXPORT; | ||
| 33 | our %EXPORT_TAGS = ( all => \@EXPORT_OK ); | ||
| 34 | |||
| 35 | use FindApp::Utils::Objects <op_{,not}equals>; | ||
| 36 | |||
| 37 | use overload map { $_ => "op_$_" } <cmp eq ne>; | ||
| 38 | use overload reverse ( | ||
| 39 | unbless => qw( "" ), | ||
| 40 | length => qw( 0+ ), | ||
| 41 | op_equals => qw( == ), | ||
| 42 | op_notequals => qw( != ), | ||
| 43 | op_spaceship => qw( <=> ), # now with 👽 wormholes! | ||
| 44 | op_plus => qw( + ), | ||
| 45 | op_minus => qw( - ), | ||
| 46 | op_neg => qw( neg ), | ||
| 47 | ); | ||
| 48 | |||
| 49 | # Exported constructor via string, defaults to caller's package | ||
| 50 | # and imposes scalar context on its argument so you can pass | ||
| 51 | # it PACKAGE(caller) more easily. You can call it as &PACKAGE | ||
| 52 | # to circumvent the prototype so you can pass multiple bits | ||
| 53 | # to the constructor, as in | ||
| 54 | # &PACKAGE(map {ucfirst} <three blind mice>) | ||
| 55 | # To get a Three::Blind::Mice object back without being forced to type | ||
| 56 | # new FindApp::Utils::Package::Object map {ucfirst} <three blind mice> | ||
| 57 | sub PACKAGE(;$) { | ||
| 58 | __PACKAGE__ -> new(@_ ? @_ : scalar caller); | ||
| 59 | } | ||
| 60 | |||
| 61 | # And this one goes the other way, mostly for mapping. | ||
| 62 | # This is just the functional version of the method, | ||
| 63 | # with a builtin default argument of $_ for convenience. | ||
| 64 | sub UNPACKAGE(_) { | ||
| 65 | my($package) = @_; | ||
| 66 | my $unpackage = $package->unbless; | ||
| 67 | if (defined wantarray) { return $unpackage } | ||
| 68 | else { $_[0] = $unpackage } | ||
| 69 | } | ||
| 70 | |||
| 71 | # Constructor via string, defaults to caller's package. | ||
| 72 | sub new { | ||
| 73 | my $invocant = shift; | ||
| 74 | my $class = blessed($invocant) || $invocant; | ||
| 75 | return $invocant->new(join "::", @_) if @_ > 1; | ||
| 76 | #my $path = (@_ ? shift : caller) || subcroak "no path"; | ||
| 77 | my $path = @_ ? shift : "main"; # subcroak "no path"; | ||
| 78 | $path = blessed($path) if blessed($path); | ||
| 79 | my @self = grep { length } split /(?:::|')/, $path; | ||
| 80 | shift @self while @self > 1 && $self[0] eq "main"; | ||
| 81 | bless \@self, $class; | ||
| 82 | } | ||
| 83 | |||
| 84 | ################################################################ | ||
| 85 | |||
| 86 | sub op_eq($$$) :method { | ||
| 87 | my($this, $that, $swapped) = @_; | ||
| 88 | use locale; | ||
| 89 | return "$this" eq "$that" | ||
| 90 | } | ||
| 91 | |||
| 92 | sub op_ne($$$) :method { !&op_eq } | ||
| 93 | |||
| 94 | sub op_cmp($$$) :method { | ||
| 95 | my($this, $that, $swapped) = @_; | ||
| 96 | no warnings "uninitialized"; | ||
| 97 | no overloading; | ||
| 98 | for my $i (0 .. max $#$this, $#$that) { | ||
| 99 | return $this->[$i] cmp $that->[$i] | ||
| 100 | unless $this->[$i] eq $that->[$i]; | ||
| 101 | } | ||
| 102 | return 0; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub op_spaceship($$$) :method { | ||
| 106 | my($this, $that, $swapped) = @_; | ||
| 107 | no overloading; | ||
| 108 | return @$this <=> @$that | ||
| 109 | unless @$this == @$that; | ||
| 110 | goto \&op_cmp; # 👽 spaceships are permitted to use wormholes for speed | ||
| 111 | } | ||
| 112 | |||
| 113 | sub op_plus($$$) :method { | ||
| 114 | my($this, $that, $swapped) = @_; | ||
| 115 | if (!blessed($that) && looks_like_number($that)) { | ||
| 116 | return $this->length + $that; | ||
| 117 | } | ||
| 118 | unless($swapped) { | ||
| 119 | return $this->add($that); | ||
| 120 | } | ||
| 121 | $that = PACKAGE($that) unless blessed $that; | ||
| 122 | return $that->add($this); | ||
| 123 | } | ||
| 124 | |||
| 125 | sub op_minus($$$) :method { | ||
| 126 | my($this, $that, $swapped) = @_; | ||
| 127 | return $this if $that == 0; | ||
| 128 | return $this->super($that); | ||
| 129 | } | ||
| 130 | |||
| 131 | sub op_neg($) :method { | ||
| 132 | my($this) = @_; | ||
| 133 | return $this->left; | ||
| 134 | } | ||
| 135 | |||
| 136 | ################################################################ | ||
| 137 | |||
| 138 | # this is useful in parallel situations (see subpackage, etc) | ||
| 139 | sub self :method { | ||
| 140 | my $self = shift; | ||
| 141 | @_ == 0 || subcroak "no arguments allowed"; | ||
| 142 | return $self; | ||
| 143 | } | ||
| 144 | |||
| 145 | *object = \&self; | ||
| 146 | |||
| 147 | sub class :method { | ||
| 148 | my $self = shift; | ||
| 149 | @_ == 0 || subcroak "no arguments allowed"; | ||
| 150 | return blessed($self) || $self; | ||
| 151 | } | ||
| 152 | |||
| 153 | # Deconstructor to string | ||
| 154 | sub unbless :method { | ||
| 155 | my $self = shift; | ||
| 156 | join("::", @$self) || "main"; | ||
| 157 | } | ||
| 158 | |||
| 159 | *as_string = \&unbless; | ||
| 160 | |||
| 161 | # Deconstructor to array | ||
| 162 | sub split() :method { | ||
| 163 | my $self = shift; | ||
| 164 | @_ == 0 || subcroak "no arguments allowed"; | ||
| 165 | @$self ? @$self : "main"; | ||
| 166 | } | ||
| 167 | *as_list = \&split; | ||
| 168 | |||
| 169 | # In case I someday change the implementation. | ||
| 170 | sub aref() :method { | ||
| 171 | my $self = shift; | ||
| 172 | @_ == 0 || subcroak "no arguments allowed"; | ||
| 173 | return [ @$self ]; | ||
| 174 | } | ||
| 175 | |||
| 176 | sub length() :method { | ||
| 177 | my $self = shift; | ||
| 178 | 0 + @$self; | ||
| 179 | } | ||
| 180 | |||
| 181 | # The leftmost N portions, defaulting to 1 and maxing at length. | ||
| 182 | sub left(;$) :method { | ||
| 183 | my($self, $here) = &selfhere; | ||
| 184 | @_ == 0 || subcroak "need one argument or none"; | ||
| 185 | return $self->new("main") if $here == -@$self; | ||
| 186 | my @self = @$self; | ||
| 187 | splice @self, $here; | ||
| 188 | return $self->new(@self); | ||
| 189 | } | ||
| 190 | |||
| 191 | sub left_but(;$) :method { | ||
| 192 | my($self, $here) = &selfhere; | ||
| 193 | @_ == 0 || subcroak "need one argument or none"; | ||
| 194 | $self->left(-$here); | ||
| 195 | } | ||
| 196 | |||
| 197 | # The rightmost N portions, defaulting to 1 and maxing at length. | ||
| 198 | sub right(;$) :method { | ||
| 199 | my($self, $here) = &selfhere; | ||
| 200 | @_ == 0 || subcroak "need one argument or none"; | ||
| 201 | return $self->new("main") if $here == -@$self; | ||
| 202 | my @self = @$self; | ||
| 203 | return $self->new(splice @self, -$here); | ||
| 204 | } | ||
| 205 | |||
| 206 | sub right_but(;$) :method { | ||
| 207 | my($self, $here) = &selfhere; | ||
| 208 | @_ == 0 || subcroak "need one argument or none"; | ||
| 209 | $self->right(-$here); | ||
| 210 | } | ||
| 211 | |||
| 212 | # The N-times super-package, defaulting to 1. | ||
| 213 | *super = \&left_but; # even faster than goto :) | ||
| 214 | |||
| 215 | # Cuts the old object in two at the given position. By default | ||
| 216 | # splits at 1, so returns the dirname/superclass portion as the | ||
| 217 | # left and the basename/class portion as the right. | ||
| 218 | sub bisect(;$) : method { | ||
| 219 | my($self, $here) = &selfhere; | ||
| 220 | @_ == 0 || subcroak "need one argument or none"; | ||
| 221 | my @self = @$self; | ||
| 222 | my @right = splice @self, -$here; | ||
| 223 | my $pair = [ | ||
| 224 | $self->new(@self ? @self : "main"), | ||
| 225 | $self->new(@right ? @right : "main"), | ||
| 226 | ]; | ||
| 227 | return wantarray ? @$pair : $pair; | ||
| 228 | } | ||
| 229 | |||
| 230 | sub left_and_right(;$) : method { &bisect } | ||
| 231 | |||
| 232 | sub right_and_left(;$) : method { | ||
| 233 | my($self, $here) = &selfhere; | ||
| 234 | @_ == 0 || subcroak "need one argument or none"; | ||
| 235 | $self->bisect(-$here); | ||
| 236 | } | ||
| 237 | |||
| 238 | # Construct subpackages, one for each argument. | ||
| 239 | # Must be called in list context if multiple args. | ||
| 240 | sub add(@) :method { | ||
| 241 | my($self, @new) = @_; | ||
| 242 | my @self = @$self; | ||
| 243 | @new > 0 || subcroak "need list of subpackages to generate"; | ||
| 244 | push @self, map { ref && reftype($_) eq "ARRAY" ? @$_ : $_ } @new; | ||
| 245 | $self->new(@self); | ||
| 246 | } | ||
| 247 | |||
| 248 | sub sib(@) :method { | ||
| 249 | my($self) = shift; | ||
| 250 | @_ > 0 || subcroak "need sibling package"; | ||
| 251 | $self->super->add(@_); | ||
| 252 | } | ||
| 253 | |||
| 254 | sub add_all(@) { | ||
| 255 | my($self, @ends) = @_; | ||
| 256 | @ends > 0 || subcroak "need list of subpackages to generate"; | ||
| 257 | wantarray || @ends == 1 || subcroak "expected list context when generating multiple subpackages"; | ||
| 258 | my @packs = map { $self->add($_) } @ends; | ||
| 259 | return wantarray ? @packs : $packs[0]; | ||
| 260 | } | ||
| 261 | |||
| 262 | sub add_all_unblessed { | ||
| 263 | my @pack_obs = &add_all; | ||
| 264 | map { UNPACKAGE } @pack_obs; | ||
| 265 | } | ||
| 266 | |||
| 267 | sub span($$) :method { | ||
| 268 | @_ == 3 || subcroak "need 2 ordinals to span, not ".(@_-1)." of them"; | ||
| 269 | my($self, $start) = &selfhere; | ||
| 270 | unshift @_, $self; | ||
| 271 | my(undef, $end) = &selfhere; | ||
| 272 | |||
| 273 | # forgive them | ||
| 274 | if ($start < 0 && $end < 0 && $end < $start) { | ||
| 275 | ($start, $end) | ||
| 276 | = | ||
| 277 | ($end, $start) ; | ||
| 278 | } | ||
| 279 | |||
| 280 | if ($start > 0 && $end < 0) { | ||
| 281 | |||
| 282 | } | ||
| 283 | |||
| 284 | $end >= $start || subcroak "end argument $end should not be less than start argument $start"; | ||
| 285 | $end * $start > 0 || subcroak "span arguments $start and $end should have same sign"; | ||
| 286 | if ($start > 0) { for ($start, $end) { $_-- } } | ||
| 287 | $self->new( @$self [ $start .. $end ] ); | ||
| 288 | } | ||
| 289 | |||
| 290 | sub slice(@) :method { | ||
| 291 | @_ > 1 || subcroak "need one or more arguments"; | ||
| 292 | my($self, @here) = @_; | ||
| 293 | for my $here (@here) { | ||
| 294 | (undef, $here) = selfhere($self, $here); | ||
| 295 | $here-- if $here > 0; | ||
| 296 | } | ||
| 297 | $self->new(@$self[@here]); | ||
| 298 | } | ||
| 299 | |||
| 300 | sub snip($;$) :method { | ||
| 301 | @_ == 2 || @_ == 3 || subcroak "need either one or two arguments"; | ||
| 302 | my($self, $here) = &selfhere; | ||
| 303 | $here-- if $here > 0; | ||
| 304 | my @self = @$self; | ||
| 305 | if (my $end = shift) { | ||
| 306 | my $count = $end - $here; | ||
| 307 | $count += @self if $count < 0; | ||
| 308 | #warn "splice(@self, $here, $count);"; | ||
| 309 | splice(@self, $here, $count); | ||
| 310 | } else { | ||
| 311 | splice(@self, $here); | ||
| 312 | } | ||
| 313 | $self->new(@self); | ||
| 314 | } | ||
| 315 | |||
| 316 | sub splice($;$@) :method { | ||
| 317 | @_ >= 2 || subcroak "need at least one argument"; | ||
| 318 | my($self, $here) = &selfhere; | ||
| 319 | $here-- if $here > 0; | ||
| 320 | my @self = @$self; | ||
| 321 | if (my($count, @new) = @_) { | ||
| 322 | splice(@self, $here, $count, @new); | ||
| 323 | } else { | ||
| 324 | splice(@self, $here); | ||
| 325 | } | ||
| 326 | $self->new(@self); | ||
| 327 | } | ||
| 328 | |||
| 329 | |||
| 330 | # Why you would ever want to do this, I have no idea. | ||
| 331 | sub reverse($) :method { | ||
| 332 | my $self = shift; | ||
| 333 | @_ == 0 || subcroak "no arguments allowed"; | ||
| 334 | $self->new(CORE::reverse $self->split); | ||
| 335 | } | ||
| 336 | |||
| 337 | # Returns string, not object. | ||
| 338 | sub join($) :method { | ||
| 339 | @_ == 2 || subcroak "need one arg, not " . (@_-1); | ||
| 340 | my $self = shift; | ||
| 341 | my $separator = shift; | ||
| 342 | return CORE::join($separator, $self->split); | ||
| 343 | } | ||
| 344 | |||
| 345 | sub map(&) :method { | ||
| 346 | @_ == 2 || subcroak "need one arg, not " . (@_-1); | ||
| 347 | my($self, $code) = @_; | ||
| 348 | $self->new(CORE::map(&$code, $self->split)); | ||
| 349 | } | ||
| 350 | |||
| 351 | # Returns new object(s)? and leaves original untouched. | ||
| 352 | sub grep(&) :method { | ||
| 353 | @_ == 2 || subcroak "need one arg, not " . (@_-1); | ||
| 354 | my($self, $code) = @_; | ||
| 355 | CORE::grep(&$code, $self->split); | ||
| 356 | } | ||
| 357 | |||
| 358 | # Returns string, not object. | ||
| 359 | sub abbreviate(;$) :method { | ||
| 360 | @_ <= 2 || subcroak "need either one argument or none, not " . (@_-1) . " of them"; | ||
| 361 | my $self = shift; | ||
| 362 | my $here = shift || 1; | ||
| 363 | my($left, $right) = $self->bisect($here); | ||
| 364 | $left->map(sub { /^(\p{Lu})/ ? lc($1) : $_ })->add($right)->join(":"); | ||
| 365 | } | ||
| 366 | |||
| 367 | # Returns string, not object. | ||
| 368 | sub pmpath($) :method { shift->join("/").".pm" } | ||
| 369 | |||
| 370 | # Returns hash ref, not object. | ||
| 371 | sub stash($) { | ||
| 372 | my $self = shift; | ||
| 373 | @_ == 0 || subcroak "no arguments allowed"; | ||
| 374 | no strict "refs"; | ||
| 375 | return \%{$self."::"}; | ||
| 376 | } | ||
| 377 | |||
| 378 | 1; | ||
| 379 | |||
| 380 | =encoding utf8 | ||
| 381 | |||
| 382 | =head1 NAME | ||
| 383 | |||
| 384 | FindApp::Utils::Package::Object - FIXME | ||
| 385 | |||
| 386 | =head1 SYNOPSIS | ||
| 387 | |||
| 388 | use FindApp::Utils::Package::Object; | ||
| 389 | |||
| 390 | =head1 DESCRIPTION | ||
| 391 | |||
| 392 | =head2 Public Methods | ||
| 393 | |||
| 394 | =head3 C<abbreviate> | ||
| 395 | |||
| 396 | Returns a string where all package elements but the last two | ||
| 397 | have been abbreviated to a single lowercase letter. Used for | ||
| 398 | debugging output when the FINDAPP_DEBUG_SHORTEN environment variable is true. | ||
| 399 | |||
| 400 | $short = PACKAGE("FindApp::Utils::Package::Object::abbreviate"); | ||
| 401 | # $short now f:u:p:Object::abbreviate | ||
| 402 | |||
| 403 | =head3 C<< add(I<LIST>) >> | ||
| 404 | |||
| 405 | Returns a new object with the given arguments added as further components. | ||
| 406 | |||
| 407 | $start = PACKAGE("Three"); | ||
| 408 | $full = $start->add("Blind", "Mice"); | ||
| 409 | # $full now Three::Blind::Mice | ||
| 410 | |||
| 411 | =head3 C<< I<OBJ_LIST> = $obj->add_all(I<LIST>) >> | ||
| 412 | |||
| 413 | Returns a list of new objects, each with the original plus one element | ||
| 414 | in the argument list appended. | ||
| 415 | |||
| 416 | $pkg = PACKAGE("Spring::Field"); | ||
| 417 | @pkgs = $pkg->add_all("Ohio", "Iowa", "Maine"); | ||
| 418 | # Spring::Field::Ohio, Spring::Field::Iowa, Spring::Field::Maine | ||
| 419 | |||
| 420 | |||
| 421 | =head3 C<< I<STR_LIST> = $obj->add_all_unblessed(I<LIST>) >> | ||
| 422 | |||
| 423 | Like C<add_all> but returning strings not objects. | ||
| 424 | |||
| 425 | =head3 aref | ||
| 426 | |||
| 427 | Returns a reference to a copy of the object's package components. | ||
| 428 | Does not allow direct access to the original. | ||
| 429 | |||
| 430 | =head3 as_list | ||
| 431 | |||
| 432 | Returns a copy of the object's package components as a list. | ||
| 433 | Does not allow direct access to the original. | ||
| 434 | |||
| 435 | This is actually as alias for I<split>. | ||
| 436 | |||
| 437 | =head3 as_string | ||
| 438 | |||
| 439 | Returns a string of the object's package components joined with C<::>. | ||
| 440 | |||
| 441 | This is actually as alias for I<unbless>. | ||
| 442 | |||
| 443 | =head3 bisect I<ORDINAL> | ||
| 444 | |||
| 445 | Return two new objects which represent the original snipped at the | ||
| 446 | given I<ORDINAL> position. The I<ORDINAL> may be either positive | ||
| 447 | or negative, operating symmetrically. | ||
| 448 | |||
| 449 | For a positive I<ORDINAL>, count off starting from the B<right> end and then snip, | ||
| 450 | putting everything to the left of however far you got into the left | ||
| 451 | object returned and putting everything past where you got to in the | ||
| 452 | right object returned. | ||
| 453 | |||
| 454 | ($left, $right) = PACKAGE("A::B::C::D::E::F")->bisect(2); | ||
| 455 | # $left is A::B::C::D | ||
| 456 | # $right is E::F | ||
| 457 | |||
| 458 | ($left, $right) = PACKAGE("A::B::C::D::E::F")->bisect(4); | ||
| 459 | # $left is A::B | ||
| 460 | # $right is C::D::E::F | ||
| 461 | |||
| 462 | For a negative I<ORDINAL>, count off starting from the B<left> end and | ||
| 463 | then snip, B<still> putting everything to the left of however far you | ||
| 464 | got into the left object returned and putting everything past where you | ||
| 465 | got to in the right object returned. | ||
| 466 | |||
| 467 | ($left, $right) = PACKAGE("A::B::C::D::E::F")->bisect(-2); | ||
| 468 | # $left is A::B | ||
| 469 | # $right is C::D::E::F | ||
| 470 | |||
| 471 | ($left, $right) = PACKAGE("A::B::C::D::E::F")->bisect(-2); | ||
| 472 | # $left is A::B::C::D | ||
| 473 | # $right is E::F | ||
| 474 | |||
| 475 | Therefore the magnitude of a positive I<ORDINAL> represents how many | ||
| 476 | elements are returned in the right object, while the magnitude of a | ||
| 477 | negative I<ORDINAL> represents how many elements are returned in the | ||
| 478 | left object. If you find this hard to remember or keep straight, | ||
| 479 | |||
| 480 | =head3 class | ||
| 481 | |||
| 482 | Returns the invocant class. | ||
| 483 | |||
| 484 | =head3 grep I<CODE_REF> | ||
| 485 | |||
| 486 | Returns a list of package components for which the code reference | ||
| 487 | expression tests true on as they were each set to C<$_> in succession. | ||
| 488 | |||
| 489 | say for PACKAGE("Red::Riding::Hood")->grep(sub{/^R/}); | ||
| 490 | # Red | ||
| 491 | # Riding | ||
| 492 | |||
| 493 | =head3 join I<STRING> | ||
| 494 | |||
| 495 | Returns a string with the package elements connected by its argument. | ||
| 496 | |||
| 497 | say PACKAGE("Red::Riding::Hood")->join("!") | ||
| 498 | # Red!Riding!Hood | ||
| 499 | |||
| 500 | =head3 left I<ORDINAL> | ||
| 501 | |||
| 502 | Returns a new object that has the leftmost I<ORDINAL> elements in it. | ||
| 503 | |||
| 504 | $head = PACKAGE("A::B::C::D::E::F")->left(2); | ||
| 505 | # $head is A::B | ||
| 506 | |||
| 507 | If a negative index is supplied, all B<but> that many | ||
| 508 | right elements are returned. | ||
| 509 | |||
| 510 | $tail = PACKAGE("A::B::C::D::E::F")->left(-2); | ||
| 511 | # $tail is A::B::C::D | ||
| 512 | |||
| 513 | This method have been called I<first> or I<top> or I<head> or even | ||
| 514 | I<pull> or I<unshift>, but I<left> seemed to be the least confusing, | ||
| 515 | especially once negatives are included. | ||
| 516 | |||
| 517 | =head3 left_and_right I<ORDINAL> | ||
| 518 | |||
| 519 | Returns two objects representing the left and right parts of the original. | ||
| 520 | |||
| 521 | When I<ORDINAL> is positive, it specifies how many elements will be in | ||
| 522 | the right return object. | ||
| 523 | |||
| 524 | When I<ORDINAL> is negative, its magnitude | ||
| 525 | specifies how many elements will be in the left return object. | ||
| 526 | |||
| 527 | ($left, $right) = PACKAGE("A::B::C::D::E::F")->left_and_right(2); | ||
| 528 | # $left is A::B::C::D | ||
| 529 | # $right is E::F | ||
| 530 | |||
| 531 | ($left, $right) = PACKAGE("A::B::C::D::E::F")->left_and_right(-2); | ||
| 532 | # $left is A::B | ||
| 533 | # $right is C::D::E::F | ||
| 534 | |||
| 535 | (Same as C<bisect>, but easier to remember.) | ||
| 536 | |||
| 537 | =head3 left_but I<ORDINAL> | ||
| 538 | |||
| 539 | Returns a new object that has all B<but> the last I<ORDINAL> elements | ||
| 540 | in it. So here you get all left bits but for the last two of them: | ||
| 541 | |||
| 542 | $front = PACKAGE("A::B::C::D::E::F")->left_but(2); | ||
| 543 | # $front is A::B::C::D | ||
| 544 | |||
| 545 | This method exists so you don't have to remember negatives; it just | ||
| 546 | calls I<left> with the sign of its argument flipped. Negatives | ||
| 547 | therefore work symmetrically: | ||
| 548 | |||
| 549 | $front = PACKAGE("A::B::C::D::E::F")->left_but(-2); | ||
| 550 | # $front is A::B | ||
| 551 | |||
| 552 | |||
| 553 | Negative ordinals mean to keep only that many rather than to discard that many: | ||
| 554 | |||
| 555 | $front = PACKAGE("A::B::C::D::E::F")->left_but(-2); | ||
| 556 | # $front is A::B | ||
| 557 | |||
| 558 | The C<left_but(I<N>)> method is really the C<left(I<-N>)> method so that you | ||
| 559 | don't have to think about negative ordinals. | ||
| 560 | |||
| 561 | =head3 length | ||
| 562 | |||
| 563 | Returns the number of package elements. | ||
| 564 | |||
| 565 | $count = PACKAGE("A::B::C::D::E::F")->length; | ||
| 566 | # $count is now 6. | ||
| 567 | |||
| 568 | This is the method used for the C<0+> operator overload. | ||
| 569 | |||
| 570 | =head3 map I<CODE_REF> | ||
| 571 | |||
| 572 | Returns a new object each of whose path components is the result | ||
| 573 | of running the given code against. | ||
| 574 | |||
| 575 | $small = PACKAGE("A::B::C")->map(sub{lc}); | ||
| 576 | # $small is a::b::c | ||
| 577 | |||
| 578 | =head3 new I<LIST> | ||
| 579 | |||
| 580 | This is the general class constructor. | ||
| 581 | |||
| 582 | $class = "FindApp::Utils::Package::Object"; | ||
| 583 | $ob = $class->new("A::B::C"); | ||
| 584 | # $ob now has length 3 | ||
| 585 | |||
| 586 | If multiple arguments are given, they concatenate: | ||
| 587 | |||
| 588 | $ob = $class->new("A" .. Z"); | ||
| 589 | # $ob has length 26 | ||
| 590 | |||
| 591 | The single-quote version of the package separator is tolerated | ||
| 592 | on input, but the canonical form is always produced on output: | ||
| 593 | |||
| 594 | $ob = $class->new("you", "shouldn't've"); | ||
| 595 | # $ob is now you::shouldn::t::ve | ||
| 596 | |||
| 597 | Because the package name C<FindApp::Utils::Package::Object> is so | ||
| 598 | long, the I<PACKAGE> alias for the constructor is normally used. | ||
| 599 | |||
| 600 | =head3 object | ||
| 601 | |||
| 602 | Returns the invocant. | ||
| 603 | |||
| 604 | =head3 op_cmp | ||
| 605 | |||
| 606 | This is the method used for the overloaded C<cmp> operator. It | ||
| 607 | runs a normal C<cmp> operation on each element left to right. | ||
| 608 | That means that "AB::CD::EF" will sort earlier than "ABC::DEF" | ||
| 609 | because "AB" sorts before "ABC". The first element that differs | ||
| 610 | is what counts. | ||
| 611 | |||
| 612 | See the L<op_spaceship> method if you want it to work the other way. | ||
| 613 | |||
| 614 | In other words, it only uses the real C<cmp> operator on each element | ||
| 615 | iteratively until one fails. It does not compare the entire package as | ||
| 616 | one string. | ||
| 617 | |||
| 618 | =head3 op_eq | ||
| 619 | |||
| 620 | This is the method used for the overloaded C<eq> operator. | ||
| 621 | It compares the two strings in full using the regular C<eq>. | ||
| 622 | |||
| 623 | =head3 op_equals | ||
| 624 | |||
| 625 | This is the method used for the overloaded C<==> operator. | ||
| 626 | It is used for checking whether two objects are the same | ||
| 627 | object by comparing their reference's addresses numerically. | ||
| 628 | It returns true if their refaddrs are the same. | ||
| 629 | |||
| 630 | It's really the method of that name imported from L<FindApp::Utils::Objects>. | ||
| 631 | |||
| 632 | =head3 op_minus | ||
| 633 | |||
| 634 | This is the method used for the overloaded binary C<-> operator, | ||
| 635 | the infix one. | ||
| 636 | It is the same as calling the I<super> method with its numeric | ||
| 637 | argument, so throws away that many elements from the right and | ||
| 638 | returns a new object. | ||
| 639 | |||
| 640 | $ob = PACKAGE("usr::bin::perl"); | ||
| 641 | print $ob - 1; | ||
| 642 | # prints usr::bin | ||
| 643 | |||
| 644 | This is also used for derived C<-=> and C<--> operators. | ||
| 645 | |||
| 646 | =head3 op_ne | ||
| 647 | |||
| 648 | This is the method used for the overloaded C<ne> operator. | ||
| 649 | It is simply the reverse of the C<eq> operator. | ||
| 650 | |||
| 651 | =head3 op_neg | ||
| 652 | |||
| 653 | This is the method used for the overloaded unary C<-> operator, | ||
| 654 | the prefix one. It's a quick way to get the leftmost element. | ||
| 655 | |||
| 656 | $ob = PACKAGE("usr::bin::perl"); | ||
| 657 | print -$ob; | ||
| 658 | # prints "usr" | ||
| 659 | |||
| 660 | =head3 op_notequals | ||
| 661 | |||
| 662 | This is the method used for the overloaded C<!=> operator. | ||
| 663 | It is used for checking whether two objects are different | ||
| 664 | objects by comparing their reference's addresses numerically. | ||
| 665 | It returns true if their refaddrs are not the same number. | ||
| 666 | |||
| 667 | It's really the method of that name imported from L<FindApp::Utils::Objects>. | ||
| 668 | |||
| 669 | =head3 op_plus | ||
| 670 | |||
| 671 | This is the method used for the overloaded binary C<+> operator. | ||
| 672 | As you might expect, it returns a new object that has the right operand | ||
| 673 | concatenated to the end of the left operand in that order. | ||
| 674 | |||
| 675 | print PACKAGE("A::B") + PACKAGE("X::Y"); | ||
| 676 | # prints A::B::X::Y | ||
| 677 | |||
| 678 | It is not associative, since ordering matters. | ||
| 679 | |||
| 680 | =head3 op_spaceship | ||
| 681 | |||
| 682 | This is the method used for the overloaded C<< <=> >> operator. | ||
| 683 | |||
| 684 | It first compares the numbers of elements in each operand as numbers, | ||
| 685 | and only if those are the same does it then go on compare the respective | ||
| 686 | pieces as strings. That means that shorter package counts sort earlier | ||
| 687 | even if they have later letters. So all one-element packages would sort | ||
| 688 | before all two-element ones, and so on. | ||
| 689 | |||
| 690 | PACKAGE("AB::CD::EF") <=> PACKAGE("ABC::DEF") # 1 | ||
| 691 | |||
| 692 | That returns 1 because the first has three elements and the second has two. | ||
| 693 | See the L<op_cmp> method if you want it to work the other way by comparing | ||
| 694 | "AB" with "ABC". | ||
| 695 | |||
| 696 | Another way to think of it is that C<< $a <=> $b >> is that is works like | ||
| 697 | |||
| 698 | $a->length <=> $b->length | ||
| 699 | || | ||
| 700 | $a cmp $b | ||
| 701 | |||
| 702 | In other words, the real C<< <=> >> operator is used only for comparing | ||
| 703 | lengths. If the lengths are the same, then C<cmp> operator is used one | ||
| 704 | package element at a time. | ||
| 705 | |||
| 706 | =head3 pmpath | ||
| 707 | |||
| 708 | This method converts the package object into a path sting by swapping | ||
| 709 | the double colons out for slashes and appending ".pm" to the end. | ||
| 710 | It's just there to give a name to this simple operation: | ||
| 711 | |||
| 712 | $obj->join("/").".pm" | ||
| 713 | |||
| 714 | =head3 reverse | ||
| 715 | |||
| 716 | Returns a new object with all its elements laid out the other | ||
| 717 | direction. | ||
| 718 | |||
| 719 | PACKAGE("AB::CD::EF")->reverse | ||
| 720 | # produces ED::CD::AB | ||
| 721 | |||
| 722 | =head3 right I<ORDINAL> | ||
| 723 | |||
| 724 | Returns a new object that has the rightmost I<ORDINAL> elements in it. | ||
| 725 | |||
| 726 | $tail = PACKAGE("A::B::C::D::E::F")->right(2); | ||
| 727 | # $head is E::F | ||
| 728 | |||
| 729 | If a negative index is supplied, all B<but> that many | ||
| 730 | left elements are returned. | ||
| 731 | |||
| 732 | $head = PACKAGE("A::B::C::D::E::F")->right(-2); | ||
| 733 | # $tail is A::B::C::D | ||
| 734 | |||
| 735 | This method have been called I<last> or I<pop> or I<tail>. | ||
| 736 | |||
| 737 | =head3 right_and_left I<ORDINAL> | ||
| 738 | |||
| 739 | Returns two objects representing the left and right parts of the | ||
| 740 | original, in that order. When I<ORDINAL> is positive, it specifies how | ||
| 741 | many elements will be in the left return object. When I<ORDINAL> is | ||
| 742 | negative, its magnitude specifies how many elements will be in the right | ||
| 743 | return object. | ||
| 744 | |||
| 745 | ($left, $right) = PACKAGE("A::B::C::D::E::F")->right_and_left(2); | ||
| 746 | # $left is A::B | ||
| 747 | # $right is C::D::E::F | ||
| 748 | |||
| 749 | ($left, $right) = PACKAGE("A::B::C::D::E::F")->right_and_left(-2); | ||
| 750 | # $left is A::B::C::D | ||
| 751 | # $right is E::F | ||
| 752 | |||
| 753 | This method exists so you don't have to think about negative ordinals, | ||
| 754 | but it is potentially confusing because you get back the left first | ||
| 755 | and the right second even though those words occur in the opposite | ||
| 756 | order in the name of the method. Just remember that whichever word | ||
| 757 | has the number near it is how many of that you get. | ||
| 758 | |||
| 759 | =head3 right_but I<ORDINAL> | ||
| 760 | |||
| 761 | Returns a new object that has all B<but> the first I<ORDINAL> elements | ||
| 762 | in it. So here you get all the right bits but for the first two of them: | ||
| 763 | |||
| 764 | $back = PACKAGE("A::B::C::D::E::F")->right_but(2); | ||
| 765 | # $back is C::D::E::F | ||
| 766 | |||
| 767 | Negative ordinals keep only that many rather than to discard that many: | ||
| 768 | |||
| 769 | $back = PACKAGE("A::B::C::D::E::F")->right_but(-2); | ||
| 770 | # $back is E::F | ||
| 771 | |||
| 772 | The C<right_but(I<N>)> method is really the C<right(I<-N>)> method so that you | ||
| 773 | don't have to think about negative ordinals. | ||
| 774 | |||
| 775 | =head3 self | ||
| 776 | |||
| 777 | Returns the object itself. Dies if invoked on a class. | ||
| 778 | |||
| 779 | =head3 sib I<NAME> | ||
| 780 | |||
| 781 | Returns a new object which is the named "sibling" package of the one specified. | ||
| 782 | So for example if your package is A::B::C, then your super is A::B and represents | ||
| 783 | your parent, and if you had a sibling named J, that would be A::B::J. | ||
| 784 | |||
| 785 | $sib = PACKAGE("A::B::C")->sib("J"); | ||
| 786 | # $sub is A::B::J | ||
| 787 | |||
| 788 | It only returns one result no matter how many arguments you supply; if | ||
| 789 | you pass more than one I<NAME>, these are each separate elements in the | ||
| 790 | one return object: | ||
| 791 | |||
| 792 | $sib = PACKAGE("A::B::C")->sib("J","K"); | ||
| 793 | $ $sib is A::B::J::K. | ||
| 794 | |||
| 795 | =head3 slice I<INDICES> | ||
| 796 | |||
| 797 | Returns a new object selecting only those elements whose ordinals you specify | ||
| 798 | in the argument list. Positive ordinals count from the left and negatives | ||
| 799 | ones from the right. | ||
| 800 | |||
| 801 | $class = "FindApp::Utils::Package::Object"; | ||
| 802 | $abc_obj = $class->new("A" .. "Z"); | ||
| 803 | # $abc_obj is A::B::C::D::E::F::G::H::I::J::K::L::M::N::O::P::Q::R::S::T::U::V::W::X::Y::Z | ||
| 804 | |||
| 805 | print $abc_obj->slice(1,3,-3,-1); | ||
| 806 | # prints A::C::X::Z | ||
| 807 | |||
| 808 | =head3 snip I<START>, I<END> | ||
| 809 | |||
| 810 | Returns a new object formed by joining portion to left of the I<START> | ||
| 811 | ordinal to the portion to the right of the I<END> ordinal in the original | ||
| 812 | package elements. | ||
| 813 | |||
| 814 | =head3 span I<START>, I<END> | ||
| 815 | |||
| 816 | Returns a new object consisting of a span of all the original package | ||
| 817 | elements starting with the I<START> ordinal and ending with the I<END> | ||
| 818 | ordinal. The second argument must not be less than the first argument. | ||
| 819 | |||
| 820 | $class = "FindApp::Utils::Package::Object"; | ||
| 821 | $abc_obj = $class->new("A" .. "Z"); | ||
| 822 | # $abc_obj is A::B::C::D::E::F::G::H::I::J::K::L::M::N::O::P::Q::R::S::T::U::V::W::X::Y::Z | ||
| 823 | |||
| 824 | print $abc_obj->span(1, 4); | ||
| 825 | # prints A::B::C::D | ||
| 826 | |||
| 827 | print $abc_obj->span(-4, -1); | ||
| 828 | # prints W::X::Y::Z | ||
| 829 | |||
| 830 | print $abc_obj->span(4, 8); | ||
| 831 | # prints D::E::F::G::H | ||
| 832 | |||
| 833 | As a special dispensation for a common mistake, if both arguments | ||
| 834 | are negative but the second is greater than the first, then you | ||
| 835 | must have reversed the arguments by accident and so the method | ||
| 836 | flips them back as though you had written them correctly. | ||
| 837 | |||
| 838 | # means span(-4, -1) | ||
| 839 | print $abc_obj->span(-1, -4); | ||
| 840 | # prints W::X::Y::Z | ||
| 841 | |||
| 842 | =head3 splice I<START>, I<END> | ||
| 843 | |||
| 844 | =head3 splice I<START> | ||
| 845 | |||
| 846 | Returns a new object with everything between I<START> | ||
| 847 | and I<END> deleted. If I<END> is omitted, everything | ||
| 848 | till the end is deleted. | ||
| 849 | |||
| 850 | =head3 split | ||
| 851 | |||
| 852 | =head3 stash | ||
| 853 | |||
| 854 | =head3 super | ||
| 855 | |||
| 856 | =head3 unbless | ||
| 857 | |||
| 858 | =head2 Exports | ||
| 859 | |||
| 860 | =over | ||
| 861 | |||
| 862 | =item PACKAGE(;$) | ||
| 863 | |||
| 864 | =item UNPACKAGE(_) | ||
| 865 | |||
| 866 | =back | ||
| 867 | |||
| 868 | =head2 Sorting | ||
| 869 | |||
| 870 | resorting the dict-sorted ABBA AB::CD A::B::C::D::E::F A::B::CD::EF A::B::CDEF A::BC::D::EF A::BCD::E::F AB::CD::EF ABC::DEF | ||
| 871 | sort {cmp} packages: A::B::C::D::E::F A::B::CD::EF A::B::CDEF A::BC::D::EF A::BCD::E::F AB::CD AB::CD::EF ABBA ABC::DEF | ||
| 872 | sort {<=>} packages: ABBA AB::CD ABC::DEF A::B::CDEF AB::CD::EF A::B::CD::EF A::BC::D::EF A::BCD::E::F A::B::C::D::E::F | ||
| 873 | |||
| 874 | |||
| 875 | =head1 EXAMPLES | ||
| 876 | |||
| 877 | =head1 ENVIRONMENT | ||
| 878 | |||
| 879 | =head1 SEE ALSO | ||
| 880 | |||
| 881 | =over | ||
| 882 | |||
| 883 | =item L<FindApp> | ||
| 884 | |||
| 885 | =back | ||
| 886 | |||
| 887 | =head1 CAVEATS AND PROVISOS | ||
| 888 | |||
| 889 | =head1 BUGS AND LIMITATIONS | ||
| 890 | |||
| 891 | =head1 HISTORY | ||
| 892 | |||
| 893 | =head1 AUTHOR | ||
| 894 | |||
| 895 | Tom Christiansen << <tchrist@perl.com> >> | ||
| 896 | |||
| 897 | =head1 LICENCE AND COPYRIGHT | ||
| 898 | |||
| 899 | Copyright (c) 2016, Tom Christiansen C<< <tchrist@perl.com> >>. | ||
| 900 | All Rights Reserved. | ||
| 901 | |||
| 902 | This module is free software; you can redistribute it and/or | ||
| 903 | modify it under the same terms as Perl itself. See L<perlartistic>. | ||
| 904 | |||
| 905 | =cut | ||
| 906 |