Created
August 7, 2013 20:29
-
-
Save prakashk/6178312 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/perl | |
use feature qw{ say state }; | |
use strict; | |
use warnings; | |
use Benchmark qw{:all}; | |
use Carp; | |
my $str = "state declares a lexically scoped variable, just like my. However, those variables will never be reinitialized, contrary to lexical variables that are reinitialized each time their enclosing block is entered."; | |
my $len = length($str); | |
sub str { $str } | |
sub outside { | |
die "outside" unless length( str() ) == $len; | |
} | |
sub use_my { | |
my $str = str(); | |
die "use_my" unless length( $str ) == $len; | |
} | |
sub use_state { | |
state $str = str(); | |
die "use_state" unless length( $str ) == $len; | |
} | |
timethese( | |
10_000_000, | |
{ | |
'outside' => sub { outside() }, | |
'use my' => sub { use_my() }, | |
'use state' => sub { use_state() }, | |
} | |
); |
Author
prakashk
commented
Aug 7, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment