Last active
December 16, 2015 09:18
-
-
Save rebx/5411440 to your computer and use it in GitHub Desktop.
perl idioms I commonly use
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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