package perl5i; ###################################### # The real code is in perl5i::2 # # Please patch that # ###################################### use strict; use parent 'perl5i::latest'; use perl5i::VERSION; our $VERSION = perl5i::VERSION->VERSION; my $Latest = perl5i::VERSION->latest; sub import { if ($0 eq '-e') { goto &perl5i::latest::import; } else { require Carp::Fix::1_25; Carp::Fix::1_25::croak(<) or make a fork (forking on github is like a branch you control) and implement it yourself. =head1 Using perl5i Because perl5i I to be incompatible in the future, you do not simply C. You must declare which major version of perl5i you are using. You do this like so: # Use perl5i major version 2 use perl5i::2; Thus the code you write with, for example, C will always remain compatible even as perl5i moves on. If you want to be daring, you can C to get the latest version. This will automatically happen if the program is C<-e>. This lets you do slightly less typing for one-liners like C If you want your module to depend on perl5i, you should depend on the versioned class. For example, depend on C and not C. See L for more information about perl5i's versioning scheme. =head1 What it does perl5i enables each of these modules and adds/changes these functions. We'll provide a brief description here, but you should look at each of their documentation for full details. =head2 The Meta Object Every object (and everything is an object) now has a meta object associated with it. Using the meta object you can ask things about the object which were previously over complicated. For example... # the object's class my $class = $obj->mo->class; # its parent classes my @isa = $obj->mo->isa; # the complete inheritance hierarchy my @complete_isa = $obj->mo->linear_isa; # the reference type of the object my $reftype = $obj->mo->reftype; A meta object is used to avoid polluting the global method space. C was chosen to avoid clashing with Moose's meta object. See L for complete details. =head2 Subroutine and Method Signatures perl5i makes it easier to declare what parameters a subroutine takes. func hello($place) { say "Hello, $place!\n"; } method get($key) { return $self->{$key}; } method new($class: %args) { return bless \%args, $class; } C and C define subroutines as C does, with some extra conveniences. The signature syntax is currently very simple. The content will be assigned from @_. This: func add($this, $that) { return $this + $that; } is equivalent to: sub add { my($this, $that) = @_; return $this + $that; } C defines a method. This is the same as a subroutine, but the first argument, the I, will be removed and made into C<$self>. method get($key) { return $self->{$key}; } sub get { my $self = shift; my($key) = @_; return $self->{$key}; } Methods have a special bit of syntax. If the first item in the siganture is C<$var:> it will change the variable used to store the invocant. method new($class: %args) { return bless $class, \%args; } is equivalent to: sub new { my $class = shift; my %args = @_; return bless $class, \%args; } Anonymous functions and methods work, too. my $code = func($message) { say $message }; Guarantees include: @_ will not be modified except by removing the invocant Future versions of perl5i will add to the signature syntax and capabilities. Planned expansions include: Signature validation Signature documentation Named parameters Required parameters Read only parameters Aliased parameters Anonymous method and function declaration Variable method and function names Parameter traits Traditional prototypes See L for more details about future expansions. The equivalencies above should only be taken for illustrative purposes, they are not guaranteed to be literally equivalent. Note that while all parameters are optional by default, the number of parameters will eventually be enforced. For example, right now this will work: func add($this, $that) { return $this + $that } say add(1,2,3); # says 3 The extra argument is ignored. In future versions of perl5i this will be a runtime error. =head3 Signature Introspection The signature of a subroutine defined with C or C can be queried by calling the C method on the code reference. func hello($greeting, $place) { say "$greeting, $place" } my $code = \&hello; say $code->signature->num_positional_params; # prints 2 Functions defined with C will not have a signature. See L for more details. =head2 Autoboxing L allows methods to be defined for and called on most unblessed variables. This means you can call methods on ordinary strings, lists and hashes! It also means perl5i can add a lot of functionality without polluting the global namespace. L wraps a lot of Perl's built in functions so they can be called as methods on unblessed variables. C<< @a->pop >> for example. =head3 alias $scalar_reference->alias( @identifiers ); @alias->alias( @identifiers ); %hash->alias( @identifiers ); (\&code)->alias( @identifiers ); Aliases a variable to a new global name. my $code = sub { 42 }; $code->alias( "foo" ); say foo(); # prints 42 It will work on everything except scalar references. our %stuff; %other_hash->alias( "stuff" ); # %stuff now aliased to %other_hash It is not a copy, changes to one will change the other. my %things = (foo => 23); our %stuff; %things->alias( "stuff" ); # alias %things to %stuff $stuff{foo} = 42; # change %stuff say $things{foo}; # and it will show up in %things Multiple @identifiers will be joined with '::' and used as the fully qualified name for the alias. my $class = "Some::Class"; my $name = "foo"; sub { 99 }->alias( $class, $name ); say Some::Class->foo; # prints 99 If there is just one @identifier and it has no "::" in it, the current caller will be prepended. C<< $thing->alias("name") >> is shorthand for C<< $thing->alias(CLASS, "name") >> Due to limitations in autobox, non-reference scalars cannot be aliased. Alias a scalar ref instead. my $thing = 23; $thing->alias("foo"); # error my $thing = \23; $thing->alias("foo"); # $foo is now aliased to $thing This is basically a nicer way to say: no strict 'refs'; *{$package . '::'. $name} = $reference; =head2 Scalar Autoboxing All of the methods provided by L are available from perl5i. in addition, perl5i adds some methods of its own. =head3 center my $centered_string = $string->center($length); my $centered_string = $string->center($length, $character); Centers $string between $character. $centered_string will be of length $length. C<$character> defaults to " ". say "Hello"->center(10); # " Hello "; say "Hello"->center(10, '-'); # "---Hello--"; C will never truncate C<$string>. If $length is less than C<< $string->length >> it will just return C<$string>. say "Hello"->center(4); # "Hello"; =head3 round my $rounded_number = $number->round; Round to the nearest integer. =head3 round_up =head3 ceil my $new_number = $number->round_up; Rounds the $number towards infinity. 2.45->round_up; # 3 (-2.45)->round_up; # -2 ceil() is a synonym for round_up(). =head3 round_down =head3 floor my $new_number = $number->round_down; Rounds the $number towards negative infinity. 2.45->round_down; # 2 (-2.45)->round_down; # -3 floor() is a synonyn for round_down(). =head3 is_number $is_a_number = $thing->is_number; Returns true if $thing is a number understood by Perl. 12.34->is_number; # true "12.34"->is_number; # also true "eleven"->is_number; # false =head3 is_positive $is_positive = $thing->is_positive; Returns true if $thing is a positive number. 0 is not positive. =head3 is_negative $is_negative = $thing->is_negative; Returns true if $thing is a negative number. 0 is not negative. =head3 is_even $is_even = $thing->is_even; Returns true if $thing is an even integer. =head3 is_odd $is_odd = $thing->is_odd; Returns true if $thing is an odd integer. =head3 is_integer $is_an_integer = $thing->is_integer; Returns true if $thing is an integer. 12->is_integer; # true 12.34->is_integer; # false "eleven"->is_integer; # false =head3 is_int A synonym for is_integer =head3 is_decimal $is_a_decimal_number = $thing->is_decimal; Returns true if $thing is a decimal number. 12->is_decimal; # false 12.34->is_decimal; # true ".34"->is_decimal; # true "point five"->is_decimal; # false =head3 require my $module = $module->require; Will C the given $module. This avoids funny things like C. It accepts only module names. On failure it will throw an exception, just like C. On a success it returns the $module. This is mostly useful so that you can immediately call $module's C method to emulate a C. # like "use $module qw(foo bar);" if that worked $module->require->import(qw(foo bar)); # like "use $module;" if that worked $module->require->import; =head3 wrap my $wrapped = $string->wrap( width => $cols, separator => $sep ); Wraps $string to width $cols, breaking lines at word boundries using separator $sep. If no width is given, $cols defaults to 76. Default line separator is the newline character "\n". See L for details. =head3 ltrim =head3 rtrim =head3 trim my $trimmed = $string->trim; my $trimmed = $string->trim($character_set); Trim whitespace. ltrim() trims off the start of the string (left), rtrim() off the end (right) and trim() off both the start and end. my $string = ' testme'->ltrim; # 'testme' my $string = 'testme '->rtrim; # 'testme' my $string = ' testme '->trim; # 'testme' They all take an optional $character_set which will determine what characters should be trimmed. It follows regex character set syntax so C will trim everything from A to Z. Defaults to C<\s>, whitespace. my $string = '-> test <-'->trim('-><'); # ' test ' =head3 title_case my $name = 'joe smith'->title_case; # Joe Smith Will uppercase every word character that follows a wordbreak character. =head3 path2module my $module = $path->path2module; Given a relative $path it will return the Perl module this represents. For example, "Foo/Bar.pm"->path2module; # "Foo::Bar" It will throw an exception if given something which could not be a path to a Perl module. =head3 module2path my $path = $module->module2path; Will return the relative $path in which the Perl $module can be found. For example, "Foo::Bar"->module2path; # "Foo/Bar.pm" =head3 is_module_name my $is_valid = $string->is_module_name; Will return true if the $string is a valid module name. "Foo::Bar"->is_module_name; # true "Foo/Bar"->is_module_name; # false =head3 group_digits my $number_grouped = $number->group_digits; my $number_grouped = $number->group_digits(\%options); Turns a number like 1234567 into a string like 1,234,567 known as "digit grouping". It honors your current locale to determine the separator and grouping. This can be overridden using C<%options>. NOTE: many systems do not have their numeric locales set properly =over 4 =item separator The character used to separate groups. Defaults to "thousands_sep" in your locale or "," if your locale doesn't specify. =item decimal_point The decimal point character. Defaults to "decimal_point" in your locale or "." if your locale does not specify. =item grouping How many numbers in a group? Defaults to "grouping" in your locale or 3 if your locale doesn't specify. Note: we don't honor the full grouping locale, its a wee bit too complicated. =item currency If true, it will treat the number as currency and use the monetary locale settings. "mon_thousands_sep" instead of "thousands_sep" and "mon_grouping" instead of "grouping". =back 1234->group_digits; # 1,234 (assuming US locale) 1234->group_digits( separator => "." ); # 1.234 =head3 commify my $number_grouped = $number->commify; my $number_grouped = $number->commify(\%options); commify() is just like group_digits() but it is not locale aware. It is useful when you want a predictable result regardless of the user's locale settings. C<%options> defaults to C<< ( separator => ",", grouping => 3, decimal_point => "." ) >>. Each key will be overridden individually. 1234->commify; # 1,234 1234->commify({ separator => "." }); # 1.234 =head3 reverse my $reverse = $string->reverse; Reverses a $string. Unlike Perl's reverse(), this always reverses the string regardless of context. =head2 Array Autoboxing The methods provided by L are available from perl5i. All the functions from L and select ones from L are all available as methods on unblessed arrays and array refs: first, max, maxstr, min, minstr, minmax, shuffle, reduce, sum, any, all, none, true, false, uniq and mesh. They have all been altered to return array refs where applicable in order to allow chaining. @array->grep(sub{ $_->is_number })->sum->say; =head3 foreach @array->foreach( func($item) { ... } ); Works like the built in C, calls the code block for each element of @array passing it into the block. @array->foreach( func($item) { say $item } ); # print each item It will pass in as many elements as the code block accepts. This allows you to iterate through an array 2 at a time, or 3 or 4 or whatever. my @names = ("Joe", "Smith", "Jim", "Dandy", "Jane", "Lane"); @names->foreach( func($fname, $lname) { say "Person: $fname $lname"; }); A normal subroutine with no signature will get one at a time. If @array is not a multiple of the iteration (for example, @array has 5 elements and you ask 2 at a time) the behavior is currently undefined. =head3 as_hash my %hash = @array->as_hash; This method returns a %hash where each element of @array is a key. The values are all true. Its functionality is similar to: my %hash = map { $_ => 1 } @array; Example usage: my @array = ("a", "b", "c"); my %hash = @array->as_hash; say q[@array contains 'a'] if $hash{"a"}; =head3 pick my @rand = @array->pick($number); The pick() method returns a list of $number elements in @array. If $number is larger than the size of the list, it returns the entire list shuffled. Example usage: my @array = (1, 2, 3, 4); my @rand = @array->pick(2); =head3 pick_one my $rand = @array->pick_one; The pick_one() method returns a random element in @array. It is similar to @array->pick(1), except that it does not return a list. Example usage: my @array = (1,2,3,4); my $rand = @array->pick_one; =head3 diff Calculate the difference between two (or more) arrays: my @a = ( 1, 2, 3 ); my @b = ( 3, 4, 5 ); my @diff_a = @a->diff(\@b) # [ 1, 2 ] my @diff_b = @b->diff(\@a) # [ 4, 5 ] Diff returns all elements in array C<@a> that are not present in array C<@b>. Item order is not considered: two identical elements in both arrays will be recognized as such disregarding their index. [ qw( foo bar ) ]->diff( [ qw( bar foo ) ] ) # empty, they are equal For comparing more than two arrays: @a->diff(\@b, \@c, ... ) All comparisons are against the base array (C<@a> in this example). The result will be composed of all those elements that were present in C<@a> and in none other. It also works with nested data structures; it will traverse them depth-first to assess whether they are identical or not. For instance: [ [ 'foo ' ], { bar => 1 } ]->diff([ 'foo' ]) # [ { bar => 1 } ] In the case of overloaded objects (i.e., L, L, L, etc.), it tries its best to treat them as strings or numbers. my $uri = URI->new("http://www.perl.com"); my $uri2 = URI->new("http://www.perl.com"); [ $uri ]->diff( [ "http://www.perl.com" ] ); # empty, they are equal [ $uri ]->diff( [ $uri2 ] ); # empty, they are equal =head3 popn my @newarray = @array->popn($n); L C<$n> values from the C<@array>. If C<$n> is greater than the length of C<@array>, it will return the whole C<@array>. If C<$n> is 0, it will return an empty array. A negative C<$n> or non-integer is an error. my @array = (1, 2, 3, 4, 5); my @newarray = @array->popn(3); # (3, 4, 5) =head3 shiftn my @newarray = @array->shiftn($n); Works like L, but it L off the front of the array instead of popping off the end. my @array = (1, 2, 3, 4, 5); my @newarray = @array->shiftn(3); # (1, 2, 3) =head3 intersect my @a = (1 .. 10); my @b = (5 .. 15); my @intersection = @a->intersect(\@b) # [ 5 .. 10 ]; Performs intersection between arrays, returning those elements that are present in all of the argument arrays simultaneously. As with C, it works with any number of arrays, nested data structures of arbitrary depth, and handles overloaded objects graciously. =head3 ltrim =head3 rtrim =head3 trim my @trimmed = @list->trim; my @trimmed = @list->trim($character_set); Trim whitespace from each element of an array. Each works just like their scalar counterpart. my @trimmed = [ ' foo', 'bar ' ]->ltrim; # [ 'foo', 'bar ' ] my @trimmed = [ ' foo', 'bar ' ]->rtrim; # [ ' foo', 'bar' ] my @trimmed = [ ' foo', 'bar ' ]->trim; # [ 'foo', 'bar' ] As with the scalar trim() methods, they all take an optional $character_set which will determine what characters should be trimmed. my @trimmed = ['-> foo <-', '-> bar <-']->trim('-><'); # [' foo ', ' bar '] =head2 Hash Autoboxing All of the methods provided by L are available from perl5i. In addition... =head3 each Iterate through each key/value pair in a hash using a callback. my %things = ( foo => 23, bar => 42 ); %things->each( func($k, $v) { say "Key: $k, Value: $v" }); Unlike the C function, individual calls to each are guaranteed to iterate through the entirety of the hash. =head3 flip Exchanges values for keys in a hash. my %things = ( foo => 1, bar => 2, baz => 5 ); my %flipped = %things->flip; # { 1 => foo, 2 => bar, 5 => baz } If there is more than one occurence of a certain value, any one of the keys may end up as the value. This is because of the random ordering of hash keys. # Could be { 1 => foo }, { 1 => bar }, or { 1 => baz } { foo => 1, bar => 1, baz => 1 }->flip; Because hash references cannot usefully be keys, it will not work on nested hashes. { foo => [ 'bar', 'baz' ] }->flip; # dies =head3 merge Recursively merge two or more hashes together using L. my $a = { a => 1 }; my $b = { b => 2, c => 3 }; $a->merge($b); # { a => 1, b => 2, c => 3 } For conflicting keys, rightmost precedence is used: my $a = { a => 1 }; my $b = { a => 100, b => 2}; $a->merge($b); # { a => 100, b => 2 } $b->merge($a); # { a => 1, b => 2 } It also works with nested hashes, although it won't attempt to merge array references or objects. For more information, look at the L docs. =head3 diff my %staff = ( bob => 42, martha => 35, timmy => 23 ); my %promoted = ( timmy => 23 ); %staff->diff(\%promoted); # { bob => 42, martha => 35 } Returns the key/value pairs present in the first hash that are not present in the subsequent hash arguments. Otherwise works as C<< @array->diff >>. =head3 intersect %staff->intersect(\%promoted); # { timmy => 23 } Returns the key/value pairs that are present simultaneously in all the hash arguments. Otherwise works as C<< @array->intersect >>. =head2 Code autoboxing =head3 signature my $sig = $code->signature; You can query the signature of any code reference defined with C or C. See L for details. If C<$code> has a signature, returns an object representing C<$code>'s signature. See L for details. Otherwise it returns nothing. =head2 caller L causes C to return an object in scalar context. =head2 die C now always returns an exit code of 255 instead of trying to use C<$!> or C<$?> which makes the exit code unpredictable. If you want to exit with a message and a special exit code, use C then C. =head2 list C will force list context similar to how L will force scalar context. =head2 utf8::all perl5i turns on L which turns on all the Unicode features of Perl it can. Here is the current list, more may be turned on later. Bare strings in your source code are now UTF8. This means UTF8 variable and method names, strings and regexes. my $message = "انا لا اتكلم العربيه"; my $τάδε = "It's all Greek to me!"; sub fünkßhüñ { ... } Strings will be treated as a set of characters rather than a set of bytes. For example, C will return the number of characters, not the number of bytes. length("perl5i is MËTÁŁ"); # 15, not 18 C<@ARGV> will be read as UTF8. STDOUT, STDIN, STDERR and all newly opened filehandles will have UTF8 encoding turned on. Consequently, if you want to output raw bytes to a file, such as outputting an image, you must set C<< binmode $fh >>. =head2 capture my($stdout, $stderr) = capture { ... } %options; my $stdout = capture { ... } %options; C lets you capture all output to C and C in any block of code. # $out = "Hello" # $err = "Bye" my($out, $err) = capture { print "Hello"; print STDERR "Bye"; }; If called in scalar context, it will only return C and silence C. # $out = "Hello" my $out = capture { print "Hello"; warn "oh god"; }; C takes some options. =over 4 =item B tee will cause output to be captured yet still printed. my $out = capture { print "Hi" } tee => 1; =item B merge will merge C and C into one variable. # $out = "HiBye" my $out = capture { print "Hi"; print STDERR "Bye"; } merge => 1; =back =head2 Carp C and C from L are always available. The Carp message will always format consistently, smoothing over the backwards incompatible change in Carp 1.25. =head2 Child L provides the C function which is a better way to do forking. C creates and starts a child process, and returns an L object which is a better interface for managing the child process. The only required argument is a codeblock, which is called in the new process. exit() is automatically called for you after the codeblock returns. my $proc = child { my $parent = shift; ... }; You can also request a pipe for IPC: my $proc = child { my $parent = shift; $parent->say("Message"); my $reply = $parent->read(); ... } pipe => 1; my $message = $proc->read(); $proc->say("reply"); See L for more information. =head2 English L gives English names to the punctuation variables; for instance, C<<$@>> is also C<<$EVAL_ERROR>>. See L for details. It does B load the regex variables which affect performance. C<$PREMATCH>, C<$MATCH>, and C<$POSTMATCH> will not exist. See the C

modifier in L for a better alternative. =head2 Modern::Perl L turns on strict and warnings, enables all the 5.10 features like C, C and C, and enables C3 method resolution order. =head2 CLASS Provides C and C<$CLASS> alternatives to C<__PACKAGE__>. =head2 File::chdir L gives you C<$CWD> representing the current working directory and it's assignable to C. You can also localize it to safely chdir inside a scope. =head2 File::stat L causes C to return an object in scalar context. =head2 DateTime C