File: Keyword.pm

package info (click to toggle)
libperlx-assert-perl 0.905-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 168 kB
  • sloc: perl: 389; makefile: 2
file content (85 lines) | stat: -rw-r--r-- 1,475 bytes parent folder | download | duplicates (4)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use 5.012000;
use strict;
use warnings;
no warnings qw( uninitialized void once );

use Keyword::Simple ();
use PerlX::Assert ();

package PerlX::Assert::Keyword;

our $AUTHORITY = 'cpan:TOBYINK';
our $VERSION   = '0.905';
our @ISA       = qw( PerlX::Assert );

sub _install_assert
{
	my $class = shift;
	my ($subname, $globals) = @_;
	my $caller = $globals->{into};
	my $active = $globals->{check};

	Keyword::Simple::define($subname, sub
	{
		my $ref = shift;
		_eat_space($ref);

		my $name;
		if ($$ref =~ /\A(qq\b|q\b|'|")/)
		{
			require Text::Balanced;
			$name = Text::Balanced::extract_quotelike($$ref);
			_eat_space($ref);
			
			if ($$ref =~ /\A,/)
			{
				substr($$ref, 0, 1) = '';
				_eat_space($ref);
				if ($$ref =~ /\A\{/)
				{
					require Carp;
					Carp::croak("Unexpected comma between assertion name and block");
				};
			}
		}
		
		substr($$ref, 0, 0) = $class->_injection(
			$active,
			$name,
			scalar($$ref =~ /\A\{/),
		);
	});
}
 
sub _eat_space
{
	my $ref = shift;
	my $X;
	while (
		($$ref =~ m{\A( \s+ )}x and $X = 1)
		or ($$ref =~ m{\A\#} and $X = 2)
	) {
		$X==2
			? ($$ref =~ s{\A\#.+?\n}{}sm)
			: (substr($$ref, 0, length($1)) = '');
	}
	return;
}

sub _injection
{
	shift;
	my ($active, $name, $do) = @_;
	$do = $do ? "do " : "";
	
	return "() and $do"
		if not $active;
	
	return "die sprintf q[Assertion failed: %s], $name unless $do"
		if defined $name;
	
	return "die q[Assertion failed] unless $do";
}

__PACKAGE__
__END__