1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
use strict;
use warnings;
package File::Util::Interface::Modern;
{
$File::Util::Interface::Modern::VERSION = '4.132140';
}
# ABSTRACT: Modern call interface to File::Util
use lib 'lib';
use File::Util::Interface::Classic qw( _myargs );
use File::Util::Definitions qw( :all );
use vars qw(
@ISA $AUTHORITY
@EXPORT_OK %EXPORT_TAGS
);
use Exporter;
$AUTHORITY = 'cpan:TOMMY';
@ISA = qw( Exporter File::Util::Interface::Classic );
@EXPORT_OK = qw(
_remove_opts
_myargs
_names_values
_parse_in
); # some of the symbols above come from File::Util::Interface::Classic but
# the _remove_opts/_names_values methods are specifically overriden in
# this package
%EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
# --------------------------------------------------------
# File::Util::Interface::Modern::_names_values()
# --------------------------------------------------------
sub _names_values {
# ignore $_[0] File::Util object reference
if ( ref $_[1] eq 'HASH' ) {
# method was called like $f->method( { name => val } )
return $_[1]
}
# ...method called like $f->methd( name => val );
goto \&File::Util::Interface::Classic::_names_values;
}
# --------------------------------------------------------
# File::Util::Interface::Modern::_remove_opts()
# --------------------------------------------------------
sub _remove_opts {
shift; # we don't need "$this" here
my $args = shift @_;
return unless ref $args eq 'ARRAY';
my @triage = @$args; @$args = ();
my $opts = { };
while ( @triage ) {
my $arg = shift @triage;
# if an argument is '', 0, or undef, it's obviously not an --option ...
push @$args, $arg and next unless $arg; # ...so give it back to the @$args
if ( UNIVERSAL::isa( $arg, 'HASH' ) ) {
# if we got hashref, then we were called with the new & improved syntax:
# e.g.- $ftl->method( arg => { opt => foo, opt2 => bar } );
#
# ...as oppsed to the classic syntax:
# e.g.- $ftl->method( arg => value, --opt1=value, --flag )
#
# the bit of code below makes it possible to support both call syntaxes
@$opts{ keys %$arg } = values %$arg; # crane lower that rover (ahhhhh)
# err, Perl flatcopy that hashref
}
elsif ( $arg =~ /^--/ ) { # got old school "--option" argument?
# it's either a bare "--option", or it's an "--option=value" pair
my ( $opt, $value ) = split /=/, $arg;
# bare version
$opts->{ $opt } = defined $value ? $value : 1;
# ^^^^^^^ if $value is undef it's a --flag, and value=1
# sanitized version, remove leading "--" ...
my $clean_name = substr $opt, 2;
# ...and replace non-alnum chars with "_" so the names can be
# referenced as hash keys without superfluous quoting and escaping
$clean_name =~ s/[^[:alnum:]]/_/g;
$opts->{ $clean_name } = defined $value ? $value : 1;
}
else {
# but if it's not an "--option" type arg, or a hashref of options,
# then give it back to the caller's @$args arrayref
push @$args, $arg;
}
}
return $opts;
}
# --------------------------------------------------------
# File::Util::Interface::Modern::_parse_in()
# --------------------------------------------------------
sub _parse_in {
my ( $this, @in ) = @_;
my $opts = $this->_remove_opts( \@in ); # always returns a hashref, given a listref
my $in = $this->_names_values( @in ); # always returns a hashref, given anything
# merge two hashrefs
@$in{ keys %$opts } = values %$opts;
return $in;
}
# --------------------------------------------------------
# File::Util::Interface::Modern::DESTROY()
# --------------------------------------------------------
sub DESTROY { }
1;
__END__
=pod
=head1 NAME
File::Util::Interface::Modern - Modern call interface to File::Util
=head1 VERSION
version 4.132140
=head1 DESCRIPTION
Provides a ::Modern-style interface for argument passing to and between the
public and private methods of File::Util.
Whereas call syntax used to only work like this:
some_method( main_arg => value, qw/ --opt=value --patern=^foo --flag / )
This module allows File::Util to work with calls that are more consistent
with current practices in Perl, like this:
some_method( main_arg => { arg => value, opt => value, flag => 1 } );
-or-
some_method( '/var/log' => { match => [ qr/.*\.log/, qr/access|error/ ] } )
Users, don't use this module by itself. It is intended for internal use only.
=cut
|