Created
May 19, 2016 08:09
-
-
Save peczenyj/83cdb92472f28b71c1584219f1c805c3 to your computer and use it in GitHub Desktop.
defer go-like in perl using Moo
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
package main; | |
use strict; | |
use warnings; | |
use feature 'say'; | |
use Foo qw(defer); | |
{ | |
say "before"; | |
my $guard = defer { say "defer" }; | |
say "after"; | |
} | |
say "end"; |
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
package Foo; | |
use Exporter 'import'; | |
use Moo; | |
use MooX::Types::MooseLike::Base qw(CodeRef); | |
has c => ( | |
is => 'ro', | |
isa => CodeRef, | |
required => 1, | |
); | |
our @EXPORT_OK = qw(defer); | |
sub defer(&){ | |
my ($callback) = @_; | |
return __PACKAGE__->new( c => $callback ); | |
} | |
sub DEMOLISH { | |
my ($self) = @_; | |
$self->c->(); | |
} | |
1; |
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
$ perl a.pl | |
before | |
after | |
defer | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment