Cheat-Meta
view release on metacpan or search on metacpan
lib/Cheat/Sheet/Util.pod view on Meta::CPAN
=head1 NAME
Cheat::Sheet::Util - Cheat sheet for utility modules
=head1 VERSION
This document describes Cheat::Sheet::Util version 0.0.5
=head1 SYNOPSIS
$ vim Cheat/Sheet::Util.perl
:syn on
=head1 DESCRIPTION
I<I only had a high school education and believe me, I had to cheat to get that. >
--Sparky Anderson
This is a collection of "cheat sheets": highly compressed, abbreviated
documentation for various modules. Each module within the bundle covers a
top-level namespace or a set of otherwise closely-related modules.
For each module, a paragraph is given, generally:
Some::Module # Short description
qw( various exportable symbols if any );
routine( $placeholder, @arguments );
$context = function( @arguments);
$object->method();
You should be able to copy and paste this into your own code,
delete what you don't need, and be on your way.
=head1 CHEATS
=over
=item * L</ Scalar::Util>
=item * L</ List::Util>
=item * L</ List::MoreUtils>
=item * L</ List::AllUtils>
=item * L</ List::Compare>
=item * L</ Hash::Util>
=back
=head2 Scalar::Util
use Scalar::Util; # General-utility scalar subroutines
use Scalar::Util qw(
weaken isweak reftype refaddr blessed isvstring readonly tainted
dualvar looks_like_number openhandle set_prototype
);
weaken $ref; # $ref will not keep @$ref, %$ref, etc. from GC
# note: copies of weak refs are not weak
$bool = isweak $ref; # true if $ref is a weak reference
$type = reftype $ref; # 'SCALAR', 'ARRAY', 'HASH', or undef
$addr = refaddr $ref; # machine address of $ref or undef
$got = blessed $ref; # class of blessed ref or undef
$bool = isvstring $s; # true if $s is a v-string
$bool = readonly $s; # true if $s is a readonly scalar
$bool = tainted $s; # true if $s is tainted
$got = dualvar $num, $string; # $got is $num or $string in context
$bool = looks_like_number $n; # true if $n can be a number
$fh = openhandle $t_fh; # $h if $t_fh is a tied or open filehandle
set_prototype $cref, $proto; # sets prototype of &$cref to $proto
## Scalar::Util
=head2 List::Util
use List::Util; # General-utility list subroutines
use List::Util qw( max maxstr min minstr first reduce shuffle sum );
$got = max @a; # returns item > than all the rest
$got = maxstr @a; # returns item gt than all the rest
$got = min @a; # returns item < than all the rest
$got = minstr @a; # returns item lt than all the rest
$got = first {$_} @a; # ~grep but returns only first true item
$got = reduce { $bool?$a:$b } @a; # returns one item; last man standing
$got = sum @a; # sum of all elements
@gots = shuffle @a; # pseudo-randomizes order of @a
# "The following are additions that have been requested..."
sub any { $_ && return 1 for @_; 0 }; # One argument is true
sub all { $_ || return 0 for @_; 1 }; # All arguments are true
sub none { $_ && return 0 for @_; 1 }; # All arguments are false
sub notall { $_ || return 1 for @_; 0 }; # One argument is false
sub true { scalar grep { $_ } @_ }; # How many elements are true
sub false { scalar grep { !$_ } @_ }; # How many elements are false
## List::Util
=head2 List::MoreUtils
use List::MoreUtils ':all'; # The stuff missing in List::Util
use List::MoreUtils qw(
any all none notall true false firstidx first_index
lastidx last_index insert_after insert_after_string
apply after after_incl before before_incl indexes
firstval first_value lastval last_value each_array
each_arrayref pairwise natatime mesh zip uniq minmax
);
# These operators take a block (~grep), setting $_ to each item in @a
# Your block should test $_ and return a $bool
$bool = any {$_} @a; # true if any test is true ( $A || $B )
$bool = all {$_} @a; # true if all tests are true ( $A && $B )
$bool = none {$_} @a; # true if all tests are false ( !$A && !$B )
$bool = notall {$_} @a; # true if any test is false ( !$A || !$B )
# # # # # # De Morgan's Laws: # # # # # # # #
# ( !$A && !$B ) == !( $A || $B ) and ( !$A || !$B ) == !( $A && $B )
# ( $A && $B ) == !( !$A || !$B ) and ( $A || $B ) == !( !$A && !$B )
$count = true {$_} @a; # how many true tests
$count = false {$_} @a; # how many false tests
$got = firstidx{$_} @a; # first item with true test
$got = first_index # ditto; alias firstidx
$got = lastidx {$_} @a; # last item with true test
$got = last_index # ditto; alias lastidx
( run in 0.942 second using v1.01-cache-2.11-cpan-99c4e6809bf )