Skip to content

Instantly share code, notes, and snippets.

@rebx
Last active December 16, 2015 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rebx/5411440 to your computer and use it in GitHub Desktop.
Save rebx/5411440 to your computer and use it in GitHub Desktop.
perl idioms I commonly use
# size of array
sub foo { return qw(foo bar baz);}
$size = () = foo();
# or just use int. scalar works as well, but int is more "intuitive" than scalar to me
$size = int (@array);
# interleave array
sub interleave {
my %interleaved;
@interleaved{ @{$_[0]} } = @{$_[1]};
return %interleaved;
}
# or
@baz = map { ( $_ , shift @bar ) } @foo;
# get last element returned from function returning a list
$s = foo();
# inverse; get the first element
($s) = foo();
# merge hash references
$s = { %{$c}, %{$d}}
# default hash value. the orchish maneuver
$s->{size} //= 0
# create a hash using qw
$s = {qw(foo 1 bar 2 baz 3)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment