test plugin loading
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / Nopaste.pm
CommitLineData
24cc824f 1package Devel::REPL::Plugin::Nopaste;
2
6a5409bc 3use Devel::REPL::Plugin;
76cd9acd 4use Moose::Util::TypeConstraints;
aa8b7647 5use namespace::autoclean;
a478012d 6use Scalar::Util qw(blessed);
24cc824f 7
3a400715 8sub BEFORE_PLUGIN {
9 my $self = shift;
10 $self->load_plugin('Turtles');
11}
24cc824f 12
13has complete_session => (
14 metaclass => 'String',
15 is => 'rw',
16 isa => 'Str',
8995c74b 17 lazy => 1,
24cc824f 18 default => '',
8bf72450 19 handles => {
20 add_to_session => 'append',
24cc824f 21 },
22);
23
da4881b1 24has paste_title => (
da4881b1 25 is => 'rw',
26 isa => 'Str',
27 lazy => 1,
28 default => 'Devel::REPL session',
29);
30
76cd9acd 31has 'nopaste_format' => (
32 is => 'rw',
33 isa => enum( [qw[ comment_code comment_output ]] ),
34 lazy => 1,
35 default => 'comment_output',
36);
37
a4c582b6 38before eval => sub {
24cc824f 39 my $self = shift;
40 my $line = shift;
41
76cd9acd 42 if ( $self->nopaste_format() eq 'comment_code' ) {
43 # prepend each line with #
44 $line =~ s/^/# /mg;
45 }
24cc824f 46
a4c582b6 47 $self->add_to_session($line . "\n");
48};
49
50around eval => sub {
51 my $orig = shift;
52 my $self = shift;
53 my $line = shift;
54
55 my @ret = $orig->($self, $line, @_);
a478012d 56 my @ret_as_str = map {
57 if (!defined($_)) {
58 '';
59 } elsif (blessed($_) && $_->can('stringify')) {
60 $_->stringify();
61 } else {
62 $_;
63 }
64 } @ret;
65
76cd9acd 66 if ( $self->nopaste_format() eq 'comment_output' ) {
67 # prepend each line with #
68 map { $_ =~ s/^/# /mg } @ret_as_str;
69 }
70
a478012d 71 $self->add_to_session(join("\n", @ret_as_str) . "\n\n");
24cc824f 72
73 return @ret;
74};
75
76sub command_nopaste {
77 my $self = shift;
78
79 require App::Nopaste;
80 return App::Nopaste->nopaste(
81 text => $self->complete_session,
da4881b1 82 desc => $self->paste_title(),
24cc824f 83 lang => "perl",
84 );
85}
86
da4881b1 87sub command_pastetitle {
88 my ( $self, undef, $title ) = @_;
89
90 $self->paste_title( $title );
91}
92
24cc824f 931;
94
cfd1094b 95__END__
96
97=head1 NAME
98
99Devel::REPL::Plugin::Nopaste - #nopaste to upload session's input and output
100
da4881b1 101=head1 COMMANDS
102
103This module provides these commands to your Devel::REPL shell:
104
105=head2 #nopaste
106
107The C<#nopaste> sends a transcript of your session to a nopaste
108site.
109
110=head2 #pastetitle
111
112The C<#pastetitle> command allows you to set the title of the paste on
113the nopaste site. For example:
114
115C<#pastetitle example of some code>
116
117defaults to 'Devel::REPL session'
118
76cd9acd 119=head1 CONFIGURATION
120
121=head2 nopaste_format
122
123The format sent to the nopaste server can be adjusted with the
124C<nopaste_format> option. By default, the output of each perl
125statement is commented out, and the perl statements themselves are
126not. This can be reversed by setting the C<nopaste_format> attribute
127to C<comment_code> like this in your re.pl file:
128
129C<< $_REPL->nopaste_format( 'comment_code' ); >>
130
131The default of commenting out the output would be set like this:
132
133C<< $_REPL->nopaste_format( 'comment_output' ); >>
134
135These options can be set during a Devel::REPL session, but only affect
136the future parts of the session, not the past parts.
137
30b459d4 138=head1 AUTHOR
139
140Shawn M Moore, C<< <sartak at gmail dot com> >>
141
da4881b1 142=head1 CONTRIBUTORS
143
144=over 4
145
146=item Andrew Moore - C<< <amoore@cpan.org> >>
147
148=back
149
cfd1094b 150=cut
151