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