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
# Tye + Juerd in id=204874
sub slurp { local( *ARGV, $/ ); @ARGV = shift; <> };
# slurp a bunch of files
@files = do { local(@ARGV, $/) = @filenames; <> };
Array/List Stuff
# 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
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|