File Coverage

blib/lib/App/QuoteCC.pm
Criterion Covered Total %
statement 12 14 85.7
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package App::QuoteCC;
2             BEGIN {
3 1     1   62278 $App::QuoteCC::AUTHORITY = 'cpan:AVAR';
4             }
5             BEGIN {
6 1     1   19 $App::QuoteCC::VERSION = '0.10';
7             }
8              
9 1     1   34 use 5.010;
  1         3  
  1         38  
10 1     1   7 use strict;
  1         2  
  1         44  
11 1     1   5 use warnings;
  1         2  
  1         38  
12 1     1   2829 use Moose;
  0            
  0            
13             use namespace::clean -except => 'meta';
14              
15             with qw/ MooseX::Getopt::Dashes /;
16              
17             # MooseX::Getopt 81b19ed83c by Karen Etheridge changed the help
18             # attribute to help_flag.
19             {
20             my @go_attrs = MooseX::Getopt::GLD->meta->get_attribute_list;
21             my $help_attr = 'help_flag' ~~ @go_attrs ? 'help_flag' : 'help';
22             has $help_attr => (
23             traits => [ qw/ Getopt / ],
24             cmd_aliases => 'h',
25             cmd_flag => 'help',
26             isa => 'Bool',
27             is => 'ro',
28             default => 0,
29             documentation => 'This help message',
30             );
31             }
32              
33             has input => (
34             traits => [ qw/ Getopt / ],
35             cmd_aliases => 'i',
36             cmd_flag => 'input',
37             isa => 'Str',
38             is => 'ro',
39             documentation => 'The quotes file to compile from. - for STDIN',
40             );
41              
42             has input_format => (
43             traits => [ qw/ Getopt / ],
44             cmd_aliases => 'I',
45             cmd_flag => 'input-type',
46             isa => 'Str',
47             is => 'ro',
48             documentation => 'The format of the input quotes file. Any App::QuotesCC::Input::*',
49             );
50              
51             has output => (
52             traits => [ qw/ Getopt / ],
53             cmd_aliases => 'o',
54             cmd_flag => 'output',
55             isa => 'Str',
56             is => 'ro',
57             default => '-',
58             documentation => 'Where to output the compiled file, - for STDOUT',
59             );
60              
61             has output_format => (
62             traits => [ qw/ Getopt / ],
63             cmd_aliases => 'O',
64             cmd_flag => 'output-type',
65             isa => 'Str',
66             is => 'ro',
67             documentation => 'The format of the output file. Any App::QuotesCC::Output::*',
68             );
69              
70             sub run {
71             my ($self) = @_;
72              
73             my $dynaload = sub {
74             my ($vars, $new_args) = @_;
75             my ($self_method_type, $class_type) = @$vars;
76             my %args = %$new_args;
77              
78             my $x_class_short = $self->$self_method_type;
79             my $x_class = "App::QuoteCC::${class_type}::" . $x_class_short;
80             {
81             my $x_class_pm = $x_class;
82             $x_class_pm =~ s[::][/]g;
83             $x_class_pm .= ".pm";
84             require $x_class_pm;
85             }
86             my $obj = $x_class->new(%args);
87             return $obj;
88             };
89              
90             my $input = $dynaload->(
91             [ qw/ input_format Input / ],
92             { file => $self->input },
93             );
94             my $quotes = $input->quotes;
95             my $output = $dynaload->(
96             [ qw/ output_format Output / ],
97             {
98             file => $self->output,
99             quotes => $quotes,
100             },
101             );
102             $output->output;
103              
104             return;
105             }
106              
107             __PACKAGE__->meta->make_immutable;
108              
109             =encoding utf8
110              
111             =head1 NAME
112              
113             App::QuoteCC - Take a quote file and emit a standalone program that spews a random quote
114              
115             =head1 SYNOPSIS
116              
117             Compile a quotes file to a stand-alone binary:
118              
119             curl http://v.nix.is/~failo/quotes.yml | quotecc -i - -I YAML -o - -O C | gcc -x c -o failo-wisdom -
120             curl http://www.trout.me.uk/quotes.txt | quotecc -i - -I Fortune -o - -O C | gcc -x c -o perl-wisdom -
121              
122             Or to a fast stand-alone minimal Perl script:
123              
124             curl http://v.nix.is/~failo/quotes.yml | quotecc -i - -I YAML -o failo-wisdom.pl -O Perl
125             curl http://www.trout.me.uk/quotes.txt | quotecc -i - -I Fortune -o perl-wisdom.pl -O Perl
126              
127             See how small they are:
128              
129             $ du -sh *-wisdom*
130             56K failo-wisdom
131             44K failo-wisdom.pl
132             80K perl-wisdom
133             76K perl-wisdom.pl
134              
135             Emit a random quote with the C program:
136              
137             time (./failo-wisdom && ./perl-wisdom)
138             Support Batman - vote for the British National Party
139             < dha> Now all I have to do is learn php
140             <@sungo> it's easy.
141             <@sungo> take your perl knowledge. now smash it against child pornography
142              
143             real 0m0.004s
144             user 0m0.000s
145             sys 0m0.008s
146              
147             Or with the Perl program:
148              
149             $ time (perl failo-wisdom.pl && perl perl-wisdom.pl)
150             I just see foreign words like private public static void feces implements shit extending penis
151             <@pndc> Imagine if cleaners were treated like sysadmins. "I've just
152             pissed all over the office floor; it's the cleaner's fault."
153              
154             real 0m0.022s
155             user 0m0.012s
156             sys 0m0.004s
157              
158             Emit all quotes:
159              
160             ./failo-wisdom --all > /tmp/quotes.txt
161              
162             Emit quotes to interactive shells on login, in F</etc/profile>:
163              
164             # spread failo's wisdom to interactive shells
165             if [[ $- == *i* ]] ; then
166             failo-wisdom
167             fi
168              
169             =head1 DESCRIPTION
170              
171             I wrote this program because using L<fortune(1)> and Perl in
172             F</etc/profile> to emit a random quote on login was too slow. On my
173             system L<fortune(1)> can take ~100 ms from a cold start, although
174             subsequent invocations when it's in cache are ~10-20 ms.
175              
176             Similarly using Perl is also slow, this is in the 80 ms range:
177              
178             perl -COEL -MYAML::XS=LoadFile -E'@q = @{ LoadFile("/path/to/quotes.yml") }; @q && say $q[rand @q]'
179              
180             Either way, when you have a 40 ms ping time to the remote machine
181             showing that quote is the major noticeable delay when you do I<ssh
182             machine>.
183              
184             L<quotecc> solves that problem, showing a quote takes around 4 ms
185             now. That's comparable with any hello wold program in C that I
186             produce.
187              
188             =head1 AUTHOR
189              
190             E<AElig>var ArnfjE<ouml>rE<eth> Bjarmason <avar@cpan.org>
191              
192             =head1 LICENSE AND COPYRIGHT
193              
194             Copyright 2010 E<AElig>var ArnfjE<ouml>rE<eth> Bjarmason <avar@cpan.org>
195              
196             This program is free software, you can redistribute it and/or modify
197             it under the same terms as Perl itself.
198              
199             =cut
200