| 1 | package Backtick::AutoChomp; | ||||||||||||
| 2 | |||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 3 | ▶ | use Filter::Simple; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 3 | ▶ | use PPI; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 3 | our $VERSION = '0.02'; | |||||||||||
| 8 | 2 | ▶ | FILTER { | ||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||
| 9 | 2 | my $doc = PPI::Document->new(\$_); | |||||||||||
| 10 | $_->set_content(sprintf 'do{local $_=%s;chomp;$_}', $_->content) | ||||||||||||
| 11 | 2 | ▶ | for @{ $doc->find( sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||
| 12 | 185 | ▶ | $_[1]->isa('PPI::Token::QuoteLike::Backtick') | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 13 | or | ||||||||||||
| 14 | $_[1]->isa('PPI::Token::QuoteLike::Command') | ||||||||||||
| 15 | 2 | ▶ | } ) || [] | ||||||||||
|
Condition100%2/2
$doc->find(sub {
$_[1]->isa('PPI::Token::QuoteLike::Backtick') or $_[1]->isa('PPI::Token::QuoteLike::Command');
}
) || []
MC/DC100%1/1
$doc->find(sub {
$_[1]->isa('PPI::Token::QuoteLike::Backtick') or $_[1]->isa('PPI::Token::QuoteLike::Command');
}
) || []
$doc->find(sub {
$_[1]->isa('PPI::Token::QuoteLike::Backtick') or $_[1]->isa('PPI::Token::QuoteLike::Command');
}
)
Truth table100%2/2
A
A= $doc->find(sub {
$_[1]->isa('PPI::Token::QuoteLike::Backtick') or $_[1]->isa('PPI::Token::QuoteLike::Command');
}
)
| |||||||||||||
| 16 | }; | ||||||||||||
| 17 | 2 | $_ = $doc->content; | |||||||||||
| 18 | 3 | }; | |||||||||||
| 19 | |||||||||||||
| 20 | 3 | 1; | |||||||||||
| 21 | |||||||||||||
| 22 | =pod | ||||||||||||
| 23 | |||||||||||||
| 24 | =head1 NAME | ||||||||||||
| 25 | |||||||||||||
| 26 | Backtick::AutoChomp - auto-chomp() result of backtick(``) and qx// | ||||||||||||
| 27 | |||||||||||||
| 28 | =head1 VERSION | ||||||||||||
| 29 | |||||||||||||
| 30 | Version 0.02 | ||||||||||||
| 31 | |||||||||||||
| 32 | =head1 SYNOPSIS | ||||||||||||
| 33 | |||||||||||||
| 34 | Automatically C<chomp()> result of a backtick (``) or qx// command. | ||||||||||||
| 35 | |||||||||||||
| 36 | $s = `echo blah` . 'stuff'; | ||||||||||||
| 37 | print $s; # blah\nstuff | ||||||||||||
| 38 | |||||||||||||
| 39 | use Backtick::AutoChomp; | ||||||||||||
| 40 | $s = `echo blah` . 'stuff'; | ||||||||||||
| 41 | print $s; # blahstuff | ||||||||||||
| 42 | no Backtick::AutoChomp; | ||||||||||||
| 43 | |||||||||||||
| 44 | =head1 DESCRIPTION | ||||||||||||
| 45 | |||||||||||||
| 46 | In bash, the shell will automatically chomp the result of a backtick call. | ||||||||||||
| 47 | |||||||||||||
| 48 | s=`whoami` # me | ||||||||||||
| 49 | echo =$s= # =me= | ||||||||||||
| 50 | echo =`whoami`= # =me= | ||||||||||||
| 51 | |||||||||||||
| 52 | In perl, we mustB<**> do: | ||||||||||||
| 53 | |||||||||||||
| 54 | $s = `whoami`; | ||||||||||||
| 55 | chomp($s); | ||||||||||||
| 56 | print "=$s="; | ||||||||||||
| 57 | |||||||||||||
| 58 | The goal of this module is for this to DWIM: | ||||||||||||
| 59 | |||||||||||||
| 60 | print "=".`whoami`."="; | ||||||||||||
| 61 | |||||||||||||
| 62 | Another case where this is potentially useful: | ||||||||||||
| 63 | |||||||||||||
| 64 | use Backtick::AutoChomp; | ||||||||||||
| 65 | printf "me(%s), host(%s), kernel(%s), date(%s)\n", | ||||||||||||
| 66 | `whoami`, | ||||||||||||
| 67 | `hostname`, | ||||||||||||
| 68 | `uname -r`, | ||||||||||||
| 69 | `date`, | ||||||||||||
| 70 | ; | ||||||||||||
| 71 | |||||||||||||
| 72 | B<**> Yes, there are pure-perl ways to do I<whoami>, I<hostname>, etc. But keep in mind programs that don't have equivalents ... and also, especially for temp/quick-n-dirty scripts, the convenience factor :) | ||||||||||||
| 73 | |||||||||||||
| 74 | Note that this is implemented as a source filter. It replaces a backtick or qx statement with a C<do{}> statement. | ||||||||||||
| 75 | |||||||||||||
| 76 | =head1 SEE ALSO | ||||||||||||
| 77 | |||||||||||||
| 78 | L<PPI>, L<Filter::Simple> | ||||||||||||
| 79 | |||||||||||||
| 80 | =head1 AUTHOR | ||||||||||||
| 81 | |||||||||||||
| 82 | David Westbrook (CPAN: davidrw), C<< <dwestbrook at gmail.com> >> | ||||||||||||
| 83 | |||||||||||||
| 84 | =head1 BUGS | ||||||||||||
| 85 | |||||||||||||
| 86 | Please report any bugs or feature requests to C<bug-backtick-chomp at rt.cpan.org>, or through | ||||||||||||
| 87 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Backtick-AutoChomp>. I will be notified, and then you'll | ||||||||||||
| 88 | automatically be notified of progress on your bug as I make changes. | ||||||||||||
| 89 | |||||||||||||
| 90 | =head1 SUPPORT | ||||||||||||
| 91 | |||||||||||||
| 92 | You can find documentation for this module with the perldoc command. | ||||||||||||
| 93 | |||||||||||||
| 94 | perldoc Backtick::AutoChomp | ||||||||||||
| 95 | |||||||||||||
| 96 | You can also look for information at: | ||||||||||||
| 97 | |||||||||||||
| 98 | =over 4 | ||||||||||||
| 99 | |||||||||||||
| 100 | =item * RT: CPAN's request tracker | ||||||||||||
| 101 | |||||||||||||
| 102 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Backtick-AutoChomp> | ||||||||||||
| 103 | |||||||||||||
| 104 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||
| 105 | |||||||||||||
| 106 | L<http://annocpan.org/dist/Backtick-AutoChomp> | ||||||||||||
| 107 | |||||||||||||
| 108 | =item * CPAN Ratings | ||||||||||||
| 109 | |||||||||||||
| 110 | L<http://cpanratings.perl.org/d/Backtick-AutoChomp> | ||||||||||||
| 111 | |||||||||||||
| 112 | =item * Search CPAN | ||||||||||||
| 113 | |||||||||||||
| 114 | L<http://search.cpan.org/dist/Backtick-AutoChomp> | ||||||||||||
| 115 | |||||||||||||
| 116 | =back | ||||||||||||
| 117 | |||||||||||||
| 118 | =head1 COPYRIGHT & LICENSE | ||||||||||||
| 119 | |||||||||||||
| 120 | Copyright 2008 David Westbrook, all rights reserved. | ||||||||||||
| 121 | |||||||||||||
| 122 | This program is free software; you can redistribute it and/or modify it | ||||||||||||
| 123 | under the same terms as Perl itself. | ||||||||||||
| 124 | |||||||||||||
| 125 | =cut | ||||||||||||
| 126 | |||||||||||||