In response to a recent post about Perl Idioms I decided to start a list of perl Idiom. Below are the few I have compiled so far. Reply or msg me with more and I will add them to the main body here. To start us off:
File Slurping
Array/List Stuff# Tye + Juerd in id=204874 sub slurp { local( *ARGV, $/ ); @ARGV = shift; <> }; # slurp a bunch of files @files = do { local(@ARGV, $/) = @filenames; <> };
# get the indexes of items in an array @foo{ @foo } = 0 .. $#foo; $foo{'abc'} # the index of 'abc' in @foo # dragonchild id=376694 sub unique { my %x; grep { !$x{$_}++ } @_ } # broquaint id=374287 sub in_list {return !!grep { $_ eq $_[0] } @_[ 1 .. $#_ ];} # ysth sub in_list {my $target=shift; return !!grep { $_ eq $target } @_} # demerphq sub find_first_index { my $target=shift; push @_,$target; my $i=0; $i++ while $_[$i] ne $target; return $i==$#_ ? undef : $i }
In reply to Common Perl Idioms by eric256
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |