Practice of Programming

プログラム とか Linuxとかの話題

Util::Any 0.20

In this version, 2 features are added.

  1. able to specify default kind of exports.
  2. able to define the kind which doesn't export anything, but executes code.

default exports

example:

package Your::Utils;

use Util::Any -Base;
our $Utils = Clone::clone $Util::Any::Utils;
sub _default_kinds { '-list' }
1;

You can use list functions, only using Your::Utils.

use Your::Utils; # no arguments are needed.

my @array = uniq qw/1 1 3 3/;

execute code without export anything.

the following definition.

our $Utils = {
  '-argv' => [
    [
      'Encode::Argv', '',
      {
        '-select' => [],
        # If specify "." as function name, execute code without exporting anyting.
        '.' => sub {
            my($pkg, $class, $func, $args, $kind_args) = @_;
            my($in, $to) = ('utf8', '');
            if (ref $kind_args eq 'ARRAY') {
                ($in, $to) = @$kind_args;
            }
            elsif (ref $kind_args eq 'HASH') {
                $in = $kind_args->{'in'} || 'utf8';
                $to = $kind_args->{'to'};
            }
            else {
                $in = $kind_args;
            }
            Encode::Argv->import($in, $to ? $to : ());
        }
      }
    ]
  ],
}

use '-argv' and @ARGV is turned utf8-flag on.

use Util::Yours -argv;

print utf8::is_utf8($ARGV[0]); # 1

use both 2 feature.

package Your::Utils;

use Util::Any -Base;

our $Utils = {
  '-modern' => [
    [
      'strict', '',
      {
        '-select' => [],
        '.' => sub {
                warnings->import;
                strict->import;
                if ($] >= 5.01) {
                    require feature;
                    feature->import(':5.10');
                }
        }
      }
    ]
  ],
 };

sub _default_kinds { '-modern' }

1;

And write the following code:

use Util::Yours;

$x = "";

This occurs the following error.

Global symbol "$x" requires explicit package name at ...

Because You::Utils's default kind is '-modern', and it executes the following code.

                warnings->import;
                strict->import;
                if ($] >= 5.01) {
                    require feature;
                    feature->import(':5.10');
                }