File: Code.pm

package info (click to toggle)
libextutils-builder-perl 0.020-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 280 kB
  • sloc: perl: 1,391; makefile: 2
file content (105 lines) | stat: -rw-r--r-- 2,609 bytes parent folder | download
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package ExtUtils::Builder::Action::Code;
$ExtUtils::Builder::Action::Code::VERSION = '0.020';
use strict;
use warnings;

use parent 'ExtUtils::Builder::Action::Perl';

use Carp ();
use ExtUtils::Builder::Util 'get_perl';

sub new {
	my ($class, %args) = @_;
	Carp::croak('Need to define code') if !$args{code};
	$args{modules} //= [];
	my $self = $class->SUPER::new(%args);
	return $self;
}

sub modules {
	my $self = shift;
	return @{ $self->{modules} };
}

sub execute {
	my ($self, %opts) = @_;
	my $code = $self->to_code();
	if (!$opts{quiet}) {
		my $message = $self->{message} // $code;
		print "$message\n";
	}
	eval $code . '; 1' or die $@;
	return;
}

sub to_code {
	my ($self, %opts) = @_;
	my @modules = $opts{skip_loading} ? () : map { "require $_" } $self->modules;
	return join '; ', @modules, $self->{code};
}

sub to_command {
	my ($self, %opts) = @_;
	my @modules = map { "-M$_" } $self->modules;
	my $perl = $opts{perl} // get_perl(%opts);
	return [ $perl, @modules, '-e', $self->to_code(skip_loading => 'main') ];
}

1;

#ABSTRACT: Action objects for perl code

__END__

=pod

=encoding UTF-8

=head1 NAME

ExtUtils::Builder::Action::Code - Action objects for perl code

=head1 VERSION

version 0.020

=head1 SYNOPSIS

 my $action = ExtUtils::Builder::Action::Code->new(
     code      => 'Frob::nicate(@_)',
     modules   => ['Frob'],
     message   => 'frobnicateing foo',
 );
 $action->execute(target => 'bar');
 say "Executed: ", join ' ', @$_, target => 'bar' for $action->to_command;

=head1 DESCRIPTION

This is a primitive action object wrapping a piece of perl code. The easiest way to use it is to execute it immediately. For more information on using actions, see L<ExtUtils::Builder::Action|ExtUtils::Builder::Action>. The core attributes are code and serialized, though only one of them must be given, both is strongly recommended.

=head1 ATTRIBUTES

=head2 code

This is a code-ref containing the action. On execution, it is passed the arguments of the execute method; when run as command it is passed @ARGV. In either case, C<arguments> is also passed. Of not given, it is C<eval>ed from C<serialized>.

=head2 modules

This is an optional list of modules that will be dynamically loaded before the action is run in any way. This attribute is optional.

=for Pod::Coverage new
to_code
modules

=head1 AUTHOR

Leon Timmermans <fawaka@gmail.com>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2013 by Leon Timmermans.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut