Cheat-Meta

 view release on metacpan or  search on metacpan

lib/Cheat/Sheet/Util.agi  view on Meta::CPAN

#=package Cheat::Sheet::Util
#=version 0.0.5
#=tagline Cheat sheet for utility modules
#=author  =  Copyright 2011 Xiong Changnian <xiong@cpan.org>   =
#=license = Free Software = Artistic License 2.0 = NO WARRANTY =
#=quote 
    I only had a high school education and believe me,
    I had to cheat to get that. 
    --Sparky Anderson

#-#========================================================================#-#
#-#=====# # UTILITY MODULES
#-#                             # [<-44 cols to end                78 cols ->]
#-#                             # #2345678901234567890123456789012345678901234
#-#
#=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
    $got  = insert_after {$_} $v => @a;   # put $v in @a after first true test        
    $got  = insert_after_string $s, $v => @a;  # insert after first item eq $s



( run in 0.394 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )