Last active
February 2, 2016 08:25
-
-
Save briandfoy/d2f482337830a8b10ca7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/Users/brian/bin/perls/perl5.22.0 | |
use v5.22; | |
use feature qw(signatures); | |
no warnings qw(experimental::signatures); | |
package Tie::Scalar::Sub { | |
use parent qw(Tie::Scalar); | |
use Carp qw(croak); | |
sub TIESCALAR ( $class, $code ) { | |
croak "You need to give me a code reference" | |
unless ref $code eq ref sub {}; | |
bless { code => $code }, $class; | |
} | |
sub FETCH ( $self ) { $self->{code}() } | |
sub STORE { | |
croak "You can't assign to this scalar"; | |
} | |
} | |
my $sub = sub () { | |
state $value = 37; | |
state $ratio = 2; | |
$value *= $ratio; | |
}; | |
tie my $scalar, 'Tie::Scalar::Sub', $sub; | |
my $count; | |
while( $count++ < 10 ) { | |
say $scalar; | |
} |
This file contains hidden or 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
use v5.22; | |
use feature qw(signatures); | |
no warnings qw(experimental::signatures); | |
package Tie::Scalar::Sub { | |
use parent qw(Tie::Scalar); | |
use Carp qw(croak); | |
sub TIESCALAR ( $class, $code ) { | |
croak "You need to give me a code reference" | |
unless ref $code eq ref sub {}; | |
bless { code => $code }, $class; | |
} | |
sub FETCH ( $self ) { $self->{code}() } | |
sub STORE { | |
croak "You can't assign to this scalar"; | |
} | |
} | |
my $sub = sub () { 10 + int( 10 * sin( time % 13 ) ) }; | |
tie my $scalar, 'Tie::Scalar::Sub', $sub; | |
my $count; | |
while( $count++ < 10 ) { | |
sleep 1; | |
say $scalar; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment