QuoteCC.pm

filter: Statementstmt 57.7% 26 / 45 Branchbran 50.0% 1 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 77.7% 7 / 9 Podpod 0.0% 0 / 1 totaltotal 59.6% 34 / 57 CC 1 SCAR 2.0
CC
1
Cov
57%
run
2.0
__ANON__
2.0
BEGIN
1.0
CRAP 1.2
« QuoteCC/Role/Output.pm ↑ Summary QuoteCC/Role/Input.pm »
1 package App::QuoteCC;
2 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 $App::QuoteCC::AUTHORITY = 'cpan:AVAR';
4 }
5 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 $App::QuoteCC::VERSION = '0.10';
7 }
8  
9 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 with qw/ MooseX::Getopt::Dashes /;
16  
17 # MooseX::Getopt 81b19ed83c by Karen Etheridge changed the help
18 # attribute to help_flag.
19 {
20 2 my @go_attrs = MooseX::Getopt::GLD->meta->get_attribute_list;
21 2 ▶my $help_attr = 'help_flag' ~~ @go_attrs ? 'help_flag' : 'help';
Branch: 1 branch
BranchTrueFalseTotal
'help_flag' ~~ \@go_attrs ? : 0 2 2
22 2 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 2 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 2 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 2 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 2 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 0 ▶sub run {
Subroutine run: not called CC=1 SCAR=6.9
Pod undocumented
71 0 my ($self) = @_;
72  
73 0 ▶ my $dynaload = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
74 0 my ($vars, $new_args) = @_;
75 0 my ($self_method_type, $class_type) = @$vars;
76 0 my %args = %$new_args;
77  
78 0 my $x_class_short = $self->$self_method_type;
79 0 my $x_class = "App::QuoteCC::${class_type}::" . $x_class_short;
80 {
81 0 my $x_class_pm = $x_class;
82 0 $x_class_pm =~ s[::][/]g;
83 0 $x_class_pm .= ".pm";
84 0 require $x_class_pm;
85 }
86 0 my $obj = $x_class->new(%args);
87 0 return $obj;
88 0 };
89  
90 0 my $input = $dynaload->(
91 [ qw/ input_format Input / ],
92 { file => $self->input },
93 );
94 0 my $quotes = $input->quotes;
95 0 my $output = $dynaload->(
96 [ qw/ output_format Output / ],
97 {
98 file => $self->output,
99 quotes => $quotes,
100 },
101 );
102 0 $output->output;
103  
104 0 return;
105 }
106  
107 2 __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  
« QuoteCC/Role/Output.pm ↑ Summary QuoteCC/Role/Input.pm »