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