Skip to content

ngrep and nmap keywords that act like grep and map but with a named variable instead of $_.

Notifications You must be signed in to change notification settings

exodist/Block-NamedVar

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NAME
    Block::NamedVar - Named variables for grep and map, for with multiple
    named list elements.

DESCRIPTION
    Gives you nmap and ngrep which are new keywords that let you do a map or
    grep. The difference is you can name the block variable instead of
    relying on $_. You can also turn custom map/grep like functions into
    keywords that act like nmap and ngrep.

    Gives you nfor which is like 'for' except you can loop over multiple
    elements of the array at once. If given a hash you can get the key and
    the value each iteration. Implemented such that it does not suffer the
    problems of each().

SYNOPSIS
        #!/usr/bin/perl
        use strict;
        use warnings;

        use Block::NamedVar;

        my %a_hash = ( a => 1, b => 2 );
        my @stuff = qw/a 1 b 2 c 3/

        nfor my ( $key, $value ) ( %a_hash ) {
            print $key, " = ", $value, "\n";
        }

        my @list = ngrep my $x { $x =~ m/^[a-zA-Z]$/ } @stuff;

        @list = nmap my $x { "updated_$x" } @stuff;

EXPORTED FUNCTIONS
    nfor my ( $vara, $varb, ... ) ( @list ) { ... }
        like 'for', except that you can take any number of elements from the
        array per iteration. Loop controls like 'next' and 'last' work as
        expected.

    nfor ( key => 'value' ) { $a == 'key', $b == 'value' }
        Special case of nfor, if no variables are specified $a and $b will
        be used, and 2 elements will be taken per iteration. Useful for
        iterating hashes. $a and $b are localized to your block.

    @out = ngrep var { $var ... } @list
    @out = ngrep $var { $var ... } @list
    @out = ngrep my $var { $var ... } @list
    @out = ngrep our $var { $var ... } @list
        Works just like grep except you specify a variable instead of using
        $_.

            # grep with lexical $x.
            @list = ngrep my $x { $x =~ m/^[a-zA-Z]$/ } @stuff;

            # grep with package variable $v
            $count = ngrep our $v { $v =~ m/^[a-zA-Z]$/ } @stuff;

            # grep with closure over existing $y
            my $y;
            $count = ngrep $y { $y =~ m/^[a-zA-Z]$/ } @stuff;

            # Shortcut for lexical variable
            # must be bareword.
            $count = ngrep thing { $thing =~ m/^[a-zA-Z]$/ } @stuff;

    @out = nmap var { $var ... } @list
    @out = nmap $var { $var ... } @list
    @out = nmap my $var { $var ... } @list
    @out = nmap our $var { $var ... } @list
        Works just like map except you specify a variable instead of using
        $_.

            # map with lexical $x
            @list = nmap my $x { "updated_$x" } @stuff;

AUTHORS
    Chad Granum [email protected]

COPYRIGHT
    Copyright (C) 2010 Chad Granum

    Block-NamedVar is free software; Standard perl licence.

    Block-NamedVar is distributed in the hope that it will be useful, but
    WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the license for
    more details.

About

ngrep and nmap keywords that act like grep and map but with a named variable instead of $_.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages