Acrux
view release on metacpan or search on metacpan
lib/Acrux/RefUtil.pm view on Meta::CPAN
package Acrux::RefUtil;
use strict;
use utf8;
=encoding utf8
=head1 NAME
Acrux::RefUtil - Pure Perl Utility functions for checking references and data
=head1 SYNOPSIS
use Acrux::RefUtil qw/ :all /;
=head1 DESCRIPTION
Pure Perl Utility functions for checking references and data
=head2 AS
The 'as' functions are introduced by the C<:as> import tag, which check
the type of passed argument and returns it as required type
=over 4
=item as_array_ref
This method returns the argument as a array reference
my $arr = as_array_ref( "foo" ); # ['foo']
my $arr = as_array_ref( "foo", "bar" ); # ['foo', 'bar']
my $arr = as_array_ref( ["foo", "bar"] ); # ['foo', 'bar']
my $arr = as_array_ref(); # []
my $arr = as_array_ref(undef); # []
my $arr = as_array_ref([undef]); # [undef]
=item as_array, as_list
This method returns argument as array-reference (see L</"as_array_ref">) or regular array (list) in list context
my $arr = as_array( "foo", "bar" ); # ['foo', 'bar']
my @arr = as_array( "foo", "bar" ); # ('foo', 'bar')
=item as_first, as_first_val
This method returns the first scalar value from argument(s)
my $foo = as_first( [qw/foo bar baz/] );
my $foo = as_first( qw/foo bar baz/ );
=item as_hash_ref
This method returns the argument as a hash reference
my $hash = as_hash_ref( {foo => 'one'} ); {foo => 'one'}
my $hash = as_hash_ref( foo => 'one', bar => 2 );
# {foo => 'one', bar => 2 }
my $hash = as_hash_ref(); # {}
my $hash = as_hash_ref(undef); # {}
=item as_hash
This method returns argument as hash-reference (see L</"as_hash_ref">) or regular hash in list context
my $hash = as_hash( "foo", "bar" ); # {'foo' => 'bar'}
( run in 0.831 second using v1.01-cache-2.11-cpan-39bf76dae61 )