summaryrefslogtreecommitdiffstats
path: root/t/try/lib/Try.pm
blob: 20325aa241e7333b37dcbbf0000f56d400e80fb9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package Try;
use strict;
use warnings;

use Try::Tiny ();

use Parse::Keyword { try => \&try_parser };
use Exporter 'import';

our @EXPORT = ('try');

sub try {
    my ($try, $catch, $finally) = @_;

    &Try::Tiny::try(
        $try,
        ($catch   ? (&Try::Tiny::catch($catch))     : ()),
        ($finally ? (&Try::Tiny::finally($finally)) : ()),
    );
}

sub try_parser {
    my ($try, $catch, $finally);

    lex_read_space;

    die "syntax error" unless lex_peek(1) eq '{';
    $try = parse_block;

    lex_read_space;

    if (lex_peek(6) =~ /^catch\b/) {
        lex_read(5);
        lex_read_space;
        die "syntax error" unless lex_peek(1) eq '{';
        $catch = parse_block;
    }

    lex_read_space;

    if (lex_peek(8) =~ /^finally\b/) {
        lex_read(7);
        lex_read_space;
        die "syntax error" unless lex_peek(1) eq '{';
        $finally = parse_block;
    }

    return (sub { ($try, $catch, $finally) }, 1);
}

1;