Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
package DB;
sub uplevel_args {
my @foo = caller($_[0]+1);
return @DB::args;
}
package BlockGiven;
use strict;
use Devel::Declare;
sub import {
my $class = shift;
my $caller = caller;
Devel::Declare->setup_for(
$caller => {
block_given => { const => \&_parser }
}
);
no strict 'refs';
*{$caller . "::block_given"} = \&block_given;
*{$caller . "::yield"} = \&yield;
}
sub _parser {
my ($Declarator, $Offset) = @_;
my $line = Devel::Declare::get_linestr;
if ("block_given?" eq substr($line, $Offset, length($Declarator) + 1)) {
substr($line, $Offset + length($Declarator), 1) = "";
Devel::Declare::set_linestr($line);
}
}
sub block_given() {
my @args = (DB::uplevel_args(1))[-1];
return (ref($args[-1]) eq "CODE")
}
sub yield {
(DB::uplevel_args(1))[-1]->(@_);
}
1;
package main;
use 5.010;
use strict;
use lib '.';
use BlockGiven;
sub add_then {
my $arg1 = shift;
my $arg2 = shift;
if (block_given?) {
yield($arg1 + $arg2);
}
};
add_then(
1, 1,
sub {
my $ans = shift;
say "1 + 1 is $ans!";
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.