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