View on
Nóirín Plunkett > perl5i-v2.11.2 > perl5ifaq


Annotate this POD

View/Report Bugs
Source   Latest Release: perl5i-v2.13.2


perl5ifaq - Frequently Asked Questions about perl5i

Using perl5i ^

What is perl5i?

perl5i is a Perl module. It is short for "Perl 5 + i". The "+ i" indicates a complex number, going off in a different direction than traditional Perl 5 development.

What's the point of perl5i?

perl5i is also about getting the defaults righter. Make Perl 5 DWIM better, without having to load a dozen Perl modules.

perl5i was originally conceived after Schwern had a conversation with a Ruby programmer who had a job writing Perl. He was listening to their complaints about Perl and providing better solutions where possible. What he found was a lot of the solutions were "go get this module from CPAN" or "turn on this pragma". use strict, use warnings, use autodie, use autobox, use List::Util, use DateTime.

Not only does this cause you to litter your code with a dozen use statements, it also requires tribal knowledge not neccessarily available to the new programmer. To use Perl 5 well requires an experienced Perl programmer looking over your shoulder, giving you advice.

For the experienced Perl 5 programmer, using perl5i means writing less boilerplate code. It means not having to decide between doing it the right way and doing it the convenient way. For example, you probably should be using a proper exception handling module, but which one? And then you have to get it and remember to load it. perl5i gives you try and catch that are always there, there's no excuse not to do it right.

In some ways, perl5i is "The Best of CPAN" in code form.

What's perl5i's relation to Perl 6?

perl5i steals liberally from Perl 6.

perl5i is not intended as a competitor to Perl 6 nor an abandonment. But it's going to be a while before Perl 6 is production stable and we've got to get some work done before Christmas.

What is perl5i's relation to Perl 5?

perl5i is in some ways a release valve for the frustration surrounding Perl 5 development, particularly with regard to Perl 5's conservative backwards compatibility requirements. Patching Perl 5 is also out of the league of most Perl programmers (and a lot of C programmers). Can't get a feature into Perl 5? Put it in perl5i.

Since it has a liberal compatibility policy, perl5i serves as a testing ground for new features. It will let the community try out new concepts in the wild and see how it works out. For example, autoboxing has been available for years but was rejected from incusion in Perl 5 in part because Perl 5 programmers do not grok the benefits of everything being an object.

Is perl5i intended for production?

Yes, the API is stable and its well tested. Its effects are mostly lexical and incompatibilities are with obscure "features" that you're probably not using.

Rather than reinventing the wheel, perl5i is mostly a wrapper around stable, well understood CPAN modules. It avoids unstable magic.

perl5i's interface is NOT compatible between major versions, but fear not! perl5i has an intentional backwards incompatibility plan so that code written for one version will continue to work even after you upgrade. Please read "Using perl5i" in perl5i for details.

What's perl5i's performance like?

perl5i tries to make you only pay for what you use. It delays loading most modules to keep startup time reasonable.

While we've been watching perl5i's weight, serious performance optimization has not begun. Interface and correctness take priority.

Autoboxed methods carry a run-time performance penalty similar to a normal method call. In general, because perl5i has to wrap much of Perl 5 it will run slower. Whether this actually effects the performance of your app should be determined by profiling your entire app and not just benchmarking individual operators.

perl5i's true performance comes out in helping the programmer write code faster and more consistently with less hand written code for common tasks. In some cases we've discovered perl5i works faster than the equivalent hand coded solution because perl5i can take advantage of very clever CPAN modules written in XS. Of course, you can do that without perl5i but we've done the research for you.

Coding with perl5i ^

Here are some ways to do traditional Perl 5 things the perl5i way.

How do I tell if something is a number?

  $thing->is_number;     # it's something Perl thinks is a number
  $thing->is_positive;   # it's a positive number
  $thing->is_negative;   # it's a negative number
  $thing->is_integer;    # it's an integer, no decimal part
  $thing->is_even;       # it's an even integer
  $thing->is_odd;        # it's an odd integer
  $thing->is_decimal;    # it's a decimal number

This will work even if $thing is a reference (they will all return false).

How do I get the difference between two arrays?

  my @diff = @array1->diff(\@array2);

Will return the elements in @array1 which are not in @array2.

How do I merge two hashes?

If you don't mind overwriting one hash, and want to do a shallow merge, then use a hash slice.

    @hash1{ keys %hash2 } = values %hash2;

If you want to do a shallow copy but want to preserve the original hashes, copy the first hash and then do the hash slice technique.

    my %merged = %hash1;
    @merged{ keys %hash2 } = values %hash2;

If you want to do a recursive merge, merging any subhashes, use the merge method.

    my %hash1 = ( a => 1,   b => { foo => 23 } );
    my %hash2 = ( a => 100, b => { bar => 42 } );

    # %hash1 is now ( a => 100, b => { foo => 23, bar => 42 } )

How can I get the unique keys from multiple hashes?

If the hashes are small, extract the keys into an array and use the uniq method.

    my @keys = (%hash1->keys, %hash2->keys);
    my @uniq = @keys->uniq;

If the hashes contain a lot of keys, you can save memory by not building the intermediate @keys.

    my %seen;
    for my $hash (\%hash1, \%hash2) {
        $hash->each( func($key) {
            $seen{$key} = 1;

    my @uniq = %seen->keys;

How do I iterate through an array more than one at a time?

Pass the foreach method a function which takes more than one parameter. foreach will iterate over the appropriate number of items.

  # Iterate two at a time.
  @array->foreach( func($x,$y) { say "x: $x, y: $y" };

See "foreach" in perl5i for details.

How do I get information about the current date?

localtime(), gmtime() and time() all return DateTime objects in scalar context.

No more mucking around with $year += 1900. It's simply:

    my $now = localtime;
    my $year = $now->year;

Or even:

    my $year = localtime->year;

The name of the current month can be gotten with:

    my $month_name = localtime->month_name;

You have the full range of DateTime features available.

How do I alias a variable?

You call the alias() method on the variable you want to alias.

Here's an example turning an anonymous subroutine into a named method.

    my $class = "Some::Class";
    my $name = "method_name";
    my $code = sub { ... };
    $code->alias($class, $name);

Some::Class->method_name will now call the $code.

This works for arrays, hashes and scalars. See "alias()" in perl5i for details.

How do I use a module from a variable?

Call the require method on that variable.

    my $module = "Some::Module";

If you want to import symbols, you can call import as well.


See "require" in perl5i for details.

How do I strip whitespace off a string?

Use the trim() method.

    my $string = "  some stuff  ";
    $string = $string->trim;  # $string is now "some stuff"

See "trim()" in perl5i for details.

How do I find information about my caller?

caller() returns an object in scalar context which you can query for information.

    my $caller = caller();
    printf "Something something something dark side at %s line %d.\n",
        $caller->filename, $caller->line;

How do I write my code in UTF8?

perl5i enables UTF8 processing of code, arguments, strings and filehandles. Working with UTF8 should just work.

How do I read/write a non-UTF8 file?

Since all filehandles are treated as UTF8, if you want to work on non-UTF8 data you will have to say so explicitly. Usually this involves calling binmode on the filehandle.

Here's an example of writing an image file.

    open my $fh, ">", $image_file;
    binmode $fh;
    print $fh $image_data;

Here's an example of Latin-1.

    open my $fh, ">", $file;
    binmode $fh, ":encoding(Latin-1)";
    print $fh $text;

If UTF8 is not to your liking you can switch the default encoding of newly opened filehandles with the open pragma.

    use open ":encoding(Latin-1)";  # new filehandles will be Latin-1
    use open ":std";                # so will STDOUT, STDERR and STDIN

See "utf8" in perl5i for details.

How do I get the name of the current class?

The $CLASS variable and CLASS constant are exported by perl5i and it contains the name of the current class.

    say "OMG! You're using class $CLASS.";

See "CLASS" in per5i for details.

How do I get the current directory?

Simply read $CWD. See "File::chdir" in perl5i for details.

How do I temporarily change the directory?

If a function has to change directory, it's polite to change it back before returning. perl5i provides local $CWD to accomplish this.

    sub do_things {
        local $CWD = "some/subdir";
        ... do unspeakable things in some/subdir ...
        return $whatever;

    chdir "/some/path";
    do_things();  # do_things operates in /some/path/some/subdir
    say $CWD;   # prints /some/path

Even if the code in do_things() dies, it will still return to the original directory.

See "File::chdir" in perl5i for details.

How do I catch an exception?

Use try/catch.

    try   { some_code() }
    catch { warn "some_code() didn't work because: $_" };

See "Try::Tiny" in perl5i for details.

How do I get the output of system?

Use capture.

    my $output = capture {
        system "command", "and", "some", "arguments";

See "capture()" in perl5i.

How can I capture STDERR?

Use capture.

    my($stdout, $stderr) = capture {
        ...anything run in here will have STDOUT and STDERR captured.

This will capture STDOUT and STDERR separately. To capture them together in one variable, use the merge option.

    my $output = capture {
        ...anything run in here will have STDOUT and STDERR captured...
    } merge => 1;

See "capture()" in perl5i.

How can I call backticks without shell processing?

You can't. What you can do instead is use capture and system with multiple arguments.

    my $output = capture {
        system $command, @options;

See "capture()" in perl5i.

How do I make my distribution depend on perl5i?

perl5i is not backwards compatible across major versions. This is why when you use perl5i you use a major version such as use perl5i::2. This guarantees that code you write will continue to work even after perl5i has changed.

When depending on perl5i, depend on the specific major version. That is, depend on perl5i::2 and not perl5i. This is because older versions will eventually be spun out into their own separate distributions to avoid cluttering the main dist. If you depend on perl5i::2 then the CPAN shell will always be able to find it.

How do I make perlcritic recognize perl5i?

perl5i turns on strict and warnings, but by default perlcritic does not recognize this. You can add perl5i to the default set of modules in your .perlcriticrc.

    equivalent_modules = perl5i::2

    equivalent_modules = perl5i::2

perl5i Development ^

Where can I find out more about perl5i?

You can follow perl5i development on Twitter at and we have discussions on IRC. Connect to and join #perl5i.

I have a great idea I want to add! How can I help?

Wonderful! Let us know. The best way is to create an issue in the issue tracker at Think of it less as an issue tracker and more of a web forum with great tagging.

Or you can send mail to

What is particularly useful to perl5i is to hear about problems you'd like solved. Tell us about a simple problem that you had to write too much code to solve, or load too many modules, or that had too many caveats.

Finally, if you just want to write some code, you can fork and work on it at Full details on our patching policy can be read at

We'd like to hear from you. Don't worry if you're doing it right, come talk with us.

Why doesn't perl5i use Moose?

We'd love to, but Moose more than doubles perl5i's startup time.

In addition, simply using Moose doesn't buy you much. Like perl5i, it is one line to fix much of Perl's OO woes. But even Moose needs fixing. What we would really like is to be able to conditionally use MooseX::Declare which fixes Perl's OO syntax as well as provides some better Moose defaults. But that has the double whammy of using Devel::Declare and Moose.

Why doesn't perl5i use Class::MOP?

Class::MOP is more about method declaration and dispatch. Our meta-object is more about things you want to do to every object but don't want to pollute the UNIVERSAL namespace with.

perl5i has too many dependencies!

That's not a question. Eventually, perl5i will look into a bundling solution to ease the dependency hell it's rapidly descending into. In general we've favored using a CPAN module over writing it ourselves so maintenance can be distributed.

We monitor the health of our dependencies and try to pick ones which are solid or fix those which fail too often.

syntax highlighting: