1#!/usr/bin/perl
2
3# PODNAME: clipfilter
4use strict;
5use warnings;
6use Clipboard;
7my %methods = (
8    -html => sub { require CGI; CGI::escapeHTML($_[0]) },
9    -uri => sub { require URI::Escape; URI::Escape::uri_escape($_[0]) },
10);
11
12my $result = filter($ARGV[0], Clipboard->paste);
13Clipboard->copy($result);
14print $result, "\n... is now in the clipboard.\n";
15
16sub filter {
17    my ($method, $data) = @_;
18    if (exists $methods{$method}) {
19        return $methods{$method}->($data);
20    } else {
21        require IPC::Open2;
22        my ($child_out, $child_in);
23        my $cmd_text = join ' ', @ARGV; # just for error message output
24        my $pid = IPC::Open2::open2($child_out, $child_in, @ARGV)
25            or die "Couldn't open pipe to `$cmd_text`: $!";
26
27        print {$child_in} $data or die "Couldn't write to `$cmd_text`: $?";
28        close $child_in or die "Couldn't close 'in' for `$cmd_text`: $?";
29        my $ret = join '', <$child_out>; # Hrmm... error handling?
30        close $child_out or die "Couldn't close 'out' for `$cmd_text`: $?";
31
32        waitpid($pid, 0);
33        die "Child error for `$cmd_text`: $?" if $? >> 8;
34        return $ret;
35    }
36}
37
38__END__
39
40=pod
41
42=encoding UTF-8
43
44=head1 NAME
45
46clipfilter - Run various conversions for your clipboard data.
47
48=head1 VERSION
49
50version 0.28
51
52=head1 USAGE
53
54    # (copy some stuff)
55    $ clipfilter -html
56    # (paste, with html entities substituted in)
57
58    # or URI-escaping:
59    $ clipfilter -uri
60
61    # or pipe through an arbitrary program, like `tac`, the backwards cat:
62    $ clipfilter tac
63    # Note: currently, this just dumps everything to open2() and reads
64    # everything back. It could possibly create a deadlock, but I haven't
65    # found the case that causes this, yet.
66
67=head1 MOTIVATION
68
69A very frequent user pattern is to copy something, edit it in some rote way,
70and then paste it back. Writing your own filter scripts will make it even more
71useful.
72
73=head1 BUGS
74
75Current weirdness when piping this to some programs, like 'wc' and 'tail'.
76I will work on this.
77
78=head1 AUTHOR
79
80Ryan King <rking@panoptic.com>
81
82=head1 COPYRIGHT
83
84Copyright (c) 2010.  Ryan King.  All rights reserved.
85
86This program is free software; you can redistribute it and/or modify it
87under the same terms as Perl itself.
88
89See L<http://www.perl.com/perl/misc/Artistic.html>
90
91=for :stopwords cpan testmatrix url bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
92
93=head1 SUPPORT
94
95=head2 Websites
96
97The following websites have more information about this module, and may be of help to you. As always,
98in addition to those websites please use your favorite search engine to discover more resources.
99
100=over 4
101
102=item *
103
104MetaCPAN
105
106A modern, open-source CPAN search engine, useful to view POD in HTML format.
107
108L<https://metacpan.org/release/Clipboard>
109
110=item *
111
112RT: CPAN's Bug Tracker
113
114The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN.
115
116L<https://rt.cpan.org/Public/Dist/Display.html?Name=Clipboard>
117
118=item *
119
120CPANTS
121
122The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution.
123
124L<http://cpants.cpanauthors.org/dist/Clipboard>
125
126=item *
127
128CPAN Testers
129
130The CPAN Testers is a network of smoke testers who run automated tests on uploaded CPAN distributions.
131
132L<http://www.cpantesters.org/distro/C/Clipboard>
133
134=item *
135
136CPAN Testers Matrix
137
138The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms.
139
140L<http://matrix.cpantesters.org/?dist=Clipboard>
141
142=item *
143
144CPAN Testers Dependencies
145
146The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.
147
148L<http://deps.cpantesters.org/?module=Clipboard>
149
150=back
151
152=head2 Bugs / Feature Requests
153
154Please report any bugs or feature requests by email to C<bug-clipboard at rt.cpan.org>, or through
155the web interface at L<https://rt.cpan.org/Public/Bug/Report.html?Queue=Clipboard>. You will be automatically notified of any
156progress on the request by the system.
157
158=head2 Source Code
159
160The code is open to the world, and available for you to hack on. Please feel free to browse it and play
161with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull
162from your repository :)
163
164L<https://github.com/shlomif/Clipboard>
165
166  git clone git://github.com/shlomif/Clipboard.git
167
168=head1 AUTHOR
169
170Shlomi Fish <shlomif@cpan.org>
171
172=head1 BUGS
173
174Please report any bugs or feature requests on the bugtracker website
175L<https://github.com/shlomif/Clipboard/issues>
176
177When submitting a bug or request, please include a test-file or a
178patch to an existing test-file that illustrates the bug or desired
179feature.
180
181=head1 COPYRIGHT AND LICENSE
182
183This software is copyright (c) 2021 by Ryan King <rking@panoptic.com>.
184
185This is free software; you can redistribute it and/or modify it under
186the same terms as the Perl 5 programming language system itself.
187
188=cut
189