← Index
NYTProf Performance Profile   « line view »
For rbm/rbm
  Run on Wed Feb 12 03:38:15 2020
Reported on Wed Feb 12 04:56:36 2020

Filename/usr/share/perl5/String/ShellQuote.pm
StatementsExecuted 660 statements in 2.59ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
33111.12ms1.30msString::ShellQuote::::_shell_quote_backendString::ShellQuote::_shell_quote_backend
3321543µs1.84msString::ShellQuote::::shell_quoteString::ShellQuote::shell_quote
6221100µs100µsString::ShellQuote::::CORE:matchString::ShellQuote::CORE:match (opcode)
835182µs82µsString::ShellQuote::::CORE:substString::ShellQuote::CORE:subst (opcode)
11119µs24µsString::ShellQuote::::BEGIN@28String::ShellQuote::BEGIN@28
1118µs48µsString::ShellQuote::::BEGIN@29String::ShellQuote::BEGIN@29
0000s0sString::ShellQuote::::croakString::ShellQuote::croak
0000s0sString::ShellQuote::::shell_comment_quoteString::ShellQuote::shell_comment_quote
0000s0sString::ShellQuote::::shell_quote_best_effortString::ShellQuote::shell_quote_best_effort
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1# $Id: ShellQuote.pm,v 1.11 2010-06-11 20:08:57 roderick Exp $
2#
3# Copyright (c) 1997 Roderick Schertler. All rights reserved. This
4# program is free software; you can redistribute it and/or modify it
5# under the same terms as Perl itself.
6
7=head1 NAME
8
9String::ShellQuote - quote strings for passing through the shell
10
11=head1 SYNOPSIS
12
13 $string = shell_quote @list;
14 $string = shell_quote_best_effort @list;
15 $string = shell_comment_quote $string;
16
17=head1 DESCRIPTION
18
19This module contains some functions which are useful for quoting strings
20which are going to pass through the shell or a shell-like object.
21
22=over
23
24=cut
25
26package String::ShellQuote;
27
28229µs229µs
# spent 24µs (19+5) within String::ShellQuote::BEGIN@28 which was called: # once (19µs+5µs) by RBM::BEGIN@18 at line 28
use strict;
# spent 24µs making 1 call to String::ShellQuote::BEGIN@28 # spent 5µs making 1 call to strict::import
292604µs289µs
# spent 48µs (8+40) within String::ShellQuote::BEGIN@29 which was called: # once (8µs+40µs) by RBM::BEGIN@18 at line 29
use vars qw($VERSION @ISA @EXPORT);
# spent 48µs making 1 call to String::ShellQuote::BEGIN@29 # spent 40µs making 1 call to vars::import
30
311400nsrequire Exporter;
32
331400ns$VERSION = '1.04';
34112µs@ISA = qw(Exporter);
351600ns@EXPORT = qw(shell_quote shell_quote_best_effort shell_comment_quote);
36
37sub croak {
38 require Carp;
39 goto &Carp::croak;
40}
41
42
# spent 1.30ms (1.12+182µs) within String::ShellQuote::_shell_quote_backend which was called 33 times, avg 39µs/call: # 33 times (1.12ms+182µs) by String::ShellQuote::shell_quote at line 119, avg 39µs/call
sub _shell_quote_backend {
433349µs my @in = @_;
443343µs my @err = ();
45
46 if (0) {
47 require RS::Handy;
48 print RS::Handy::data_dump(\@in);
49 }
50
513328µs return \@err, '' unless @in;
52
533334µs my $ret = '';
543325µs my $saw_non_equal = 0;
553391µs foreach (@in) {
563338µs if (!defined $_ or $_ eq '') {
5721µs $_ = "''";
5823µs next;
59 }
60
6131194µs3137µs if (s/\x00//g) {
# spent 37µs making 31 calls to String::ShellQuote::CORE:subst, avg 1µs/call
62 push @err, "No way to quote string containing null (\\000) bytes";
63 }
64
653119µs my $escape = 0;
66
67 # = needs quoting when it's the first element (or part of a
68 # series of such elements), as in command position it's a
69 # program-local environment setting
70
7131182µs3135µs if (/=/) {
# spent 35µs making 31 calls to String::ShellQuote::CORE:match, avg 1µs/call
72 if (!$saw_non_equal) {
73 $escape = 1;
74 }
75 }
76 else {
773128µs $saw_non_equal = 1;
78 }
79
8031149µs3165µs if (m|[^\w!%+,\-./:=@^]|) {
# spent 65µs making 31 calls to String::ShellQuote::CORE:match, avg 2µs/call
81 $escape = 1;
82 }
83
843138µs if ($escape
85 || (!$saw_non_equal && /=/)) {
86
87 # ' -> '\''
881346µs1312µs s/'/'\\''/g;
# spent 12µs making 13 calls to String::ShellQuote::CORE:subst, avg 938ns/call
89
90 # make multiple ' in a row look simpler
91 # '\'''\'''\'' -> '"'''"'
921349µs1316µs s|((?:'\\''){2,})|q{'"} . (q{'} x (length($1) / 4)) . q{"'}|ge;
# spent 16µs making 13 calls to String::ShellQuote::CORE:subst, avg 1µs/call
93
941323µs $_ = "'$_'";
951340µs139µs s/^''//;
# spent 9µs making 13 calls to String::ShellQuote::CORE:subst, avg 700ns/call
961342µs138µs s/''$//;
# spent 8µs making 13 calls to String::ShellQuote::CORE:subst, avg 600ns/call
97 }
98 }
99 continue {
100 $ret .= "$_ ";
101 }
102
1033347µs chop $ret;
10433334µs return \@err, $ret;
105}
106
107=item B<shell_quote> [I<string>]...
108
109B<shell_quote> quotes strings so they can be passed through the shell.
110Each I<string> is quoted so that the shell will pass it along as a
111single argument and without further interpretation. If no I<string>s
112are given an empty string is returned.
113
114If any I<string> can't be safely quoted B<shell_quote> will B<croak>.
115
116=cut
117
118
# spent 1.84ms (543µs+1.30) within String::ShellQuote::shell_quote which was called 33 times, avg 56µs/call: # 18 times (336µs+576µs) by Template::Stash::XS::get at line 4 of /root/tor-browser-build/input text, avg 51µs/call # 15 times (207µs+726µs) by Template::Stash::XS::get at line 8 of /root/tor-browser-build/input text, avg 62µs/call
sub shell_quote {
11933176µs331.30ms my ($rerr, $s) = _shell_quote_backend @_;
# spent 1.30ms making 33 calls to String::ShellQuote::_shell_quote_backend, avg 39µs/call
120
1213331µs if (@$rerr) {
122 my %seen;
123 @$rerr = grep { !$seen{$_}++ } @$rerr;
124 my $s = join '', map { "shell_quote(): $_\n" } @$rerr;
125 chomp $s;
126 croak $s;
127 }
12833234µs return $s;
129}
130
131=item B<shell_quote_best_effort> [I<string>]...
132
133This is like B<shell_quote>, excpet if the string can't be safely quoted
134it does the best it can and returns the result, instead of dying.
135
136=cut
137
138sub shell_quote_best_effort {
139 my ($rerr, $s) = _shell_quote_backend @_;
140
141 return $s;
142}
143
144=item B<shell_comment_quote> [I<string>]
145
146B<shell_comment_quote> quotes the I<string> so that it can safely be
147included in a shell-style comment (the current algorithm is that a sharp
148character is placed after any newlines in the string).
149
150This routine might be changed to accept multiple I<string> arguments
151in the future. I haven't done this yet because I'm not sure if the
152I<string>s should be joined with blanks ($") or nothing ($,). Cast
153your vote today! Be sure to justify your answer.
154
155=cut
156
157sub shell_comment_quote {
158 return '' unless @_;
159 unless (@_ == 1) {
160 croak "Too many arguments to shell_comment_quote "
161 . "(got " . @_ . " expected 1)";
162 }
163 local $_ = shift;
164 s/\n/\n#/g;
165 return $_;
166}
167
16814µs1;
169
170__END__
 
# spent 100µs within String::ShellQuote::CORE:match which was called 62 times, avg 2µs/call: # 31 times (65µs+0s) by String::ShellQuote::_shell_quote_backend at line 80, avg 2µs/call # 31 times (35µs+0s) by String::ShellQuote::_shell_quote_backend at line 71, avg 1µs/call
sub String::ShellQuote::CORE:match; # opcode
# spent 82µs within String::ShellQuote::CORE:subst which was called 83 times, avg 994ns/call: # 31 times (37µs+0s) by String::ShellQuote::_shell_quote_backend at line 61, avg 1µs/call # 13 times (16µs+0s) by String::ShellQuote::_shell_quote_backend at line 92, avg 1µs/call # 13 times (12µs+0s) by String::ShellQuote::_shell_quote_backend at line 88, avg 938ns/call # 13 times (9µs+0s) by String::ShellQuote::_shell_quote_backend at line 95, avg 700ns/call # 13 times (8µs+0s) by String::ShellQuote::_shell_quote_backend at line 96, avg 600ns/call
sub String::ShellQuote::CORE:subst; # opcode