| 1 | package EBook::Ishmael::ShellQuote; | ||||||||||
| 2 | 17 | ▶ | use 5.016; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 17 | our $VERSION = '2.03'; | |||||||||
| 4 | 17 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 17 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 17 | ▶ | use Exporter 'import'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 17 | our @EXPORT_OK = qw(shell_quote command_quote safe_qx); | |||||||||
| 9 | |||||||||||
| 10 | 17 | my $IS_WIN = $^O eq 'MSWin32'; | |||||||||
| 11 | |||||||||||
| 12 | sub shell_quote { | ||||||||||
| 13 | |||||||||||
| 14 | 0 | ▶ | my ($str) = @_; | ||||||||
|
Subroutine
shell_quote: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 15 | |||||||||||
| 16 | 0 | ▶ | if ($IS_WIN) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 17 | 0 | $str =~ s/(\\*)(?="|\z)/$1$1/g; | |||||||||
| 18 | 0 | $str =~ s/"/\\"/g; | |||||||||
| 19 | 0 | return $str; | |||||||||
| 20 | } else { | ||||||||||
| 21 | 0 | $str =~ s/([\$`"\\\n])/\\$1/g; | |||||||||
| 22 | 0 | return qq{"$str"}; | |||||||||
| 23 | } | ||||||||||
| 24 | |||||||||||
| 25 | } | ||||||||||
| 26 | |||||||||||
| 27 | sub command_quote { | ||||||||||
| 28 | |||||||||||
| 29 | 0 | ▶ | my ($str) = @_; | ||||||||
|
Subroutine
command_quote: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 30 | |||||||||||
| 31 | 0 | ▶ | if ($IS_WIN) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 32 | 0 | return shell_quote($str); | |||||||||
| 33 | } else { | ||||||||||
| 34 | 0 | return shell_quote($str); | |||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | sub safe_qx { | ||||||||||
| 40 | |||||||||||
| 41 | 0 | ▶ | my ($program, @args) = @_; | ||||||||
|
Subroutine
safe_qx: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 42 | |||||||||||
| 43 | open my $fh, '-|', $program, @args | ||||||||||
| 44 | 0 | ▶ | or do { $? = -1; return undef }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 0 | my $output = do { local $/; <$fh> }; | |||||||||
| 46 | 0 | close $fh; | |||||||||
| 47 | |||||||||||
| 48 | 0 | return $output; | |||||||||
| 49 | |||||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | 17 | 1; | |||||||||
| 53 | |||||||||||
| 54 | =head1 NAME | ||||||||||
| 55 | |||||||||||
| 56 | EBook::Ishmael::ShellQuote - Quote strings to be used in shell commands | ||||||||||
| 57 | |||||||||||
| 58 | =head1 SYNOPSIS | ||||||||||
| 59 | |||||||||||
| 60 | use EBook::Ishmael::ShellQuote qw(shell_quote); | ||||||||||
| 61 | |||||||||||
| 62 | my $quote = shell_quote("$ <-- literally a dollar sign"); | ||||||||||
| 63 | system "echo $quote"; | ||||||||||
| 64 | |||||||||||
| 65 | =head1 DESCRIPTION | ||||||||||
| 66 | |||||||||||
| 67 | B<EBook::Ishmael::ShellQuote> is a module that provides the C<shell_quote()> | ||||||||||
| 68 | subroutine for quoting strings to be passed as arguments to a shell command. | ||||||||||
| 69 | This is a private module, consult the L<ishmael> manual for user | ||||||||||
| 70 | documentation. | ||||||||||
| 71 | |||||||||||
| 72 | =head1 SUBROUTINES | ||||||||||
| 73 | |||||||||||
| 74 | =over 4 | ||||||||||
| 75 | |||||||||||
| 76 | =item $quoted = shell_quote($string) | ||||||||||
| 77 | |||||||||||
| 78 | Returns the double-quote-quotted version of the given string. Characters like | ||||||||||
| 79 | C<$>, C<`>, and C<"> will be escaped via a backslash, and the string will be | ||||||||||
| 80 | wrapped in double quotes. | ||||||||||
| 81 | |||||||||||
| 82 | =item $quoted = command_quote($string) | ||||||||||
| 83 | |||||||||||
| 84 | Returns quoted version of the given string suitable for using as shell command. | ||||||||||
| 85 | |||||||||||
| 86 | =item $output = safe_qx($program, [ @args ]) | ||||||||||
| 87 | |||||||||||
| 88 | Runs the given C<$program> with arguments C<@args> using the C<qx//> operator | ||||||||||
| 89 | and returns the output. The program name and arguments are quoted to prevent | ||||||||||
| 90 | unexpected word-splitting. | ||||||||||
| 91 | |||||||||||
| 92 | =back | ||||||||||
| 93 | |||||||||||
| 94 | =head1 AUTHOR | ||||||||||
| 95 | |||||||||||
| 96 | Written by Samuel Young, E<lt>samyoung12788@gmail.comE<gt>. | ||||||||||
| 97 | |||||||||||
| 98 | This project's source can be found on its | ||||||||||
| 99 | L<Codeberg Page|https://codeberg.org/1-1sam/ishmael>. Comments and pull | ||||||||||
| 100 | requests are welcome! | ||||||||||
| 101 | |||||||||||
| 102 | =head1 COPYRIGHT | ||||||||||
| 103 | |||||||||||
| 104 | Copyright (C) 2025-2026 Samuel Young | ||||||||||
| 105 | |||||||||||
| 106 | This program is free software: you can redistribute it and/or modify | ||||||||||
| 107 | it under the terms of the GNU General Public License as published by | ||||||||||
| 108 | the Free Software Foundation, either version 3 of the License, or | ||||||||||
| 109 | (at your option) any later version. | ||||||||||
| 110 | |||||||||||
| 111 | =head1 SEE ALSO | ||||||||||
| 112 | |||||||||||
| 113 | L<ishmael> | ||||||||||
| 114 | |||||||||||
| 115 | =cut | ||||||||||