-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathREADME
88 lines (66 loc) · 2.95 KB
/
README
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
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.