Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: preserve quotes after $0 + @ARGV interpolation

by bliako (Monsignor)
on Dec 13, 2019 at 14:52 UTC ( [id://11110073]=note: print w/replies, xml ) Need Help??


in reply to preserve quotes after $0 + @ARGV interpolation

Another way: write your own command launcher. That can just be a basic unix shell, e.g. see here https://www.geeksforgeeks.org/making-linux-shell-c/ or modify bash as I suggest in one of my answers to you below.

Here is a basic command launcher in Perl. WARNING: it is not secure to execute user input!

use strict; use warnings; my ($fh, $cmd); my $cmdI=0; open($fh, '>', 'log.txt'); while(1){ print ++$cmdI."> "; $cmd = <STDIN> || exit; print $fh $cmd; chomp($cmd); # this way you pass your command via the default shell with all the +interpolations happening #system($cmd) && die "failed to run command: $cmd"; # this way you avoid the default shell and launch your perl script d +irectly but # the command line is just one huge parameter (it does not break on +whitespace, like a shell would do) $cmd=~ s/^mycommand\.pl// || die "$0 : your command must always be ' +mycommand.pl'\n"; system('mycommand.pl', $cmd); }
#!perl # mycommand.pl print "got params (number of params: ".@ARGV.") : " . join(' ', @ARGV) +."\n";

It will first log what you wrote without interpolation or checking if quotes balance, etc. Then it can pass what you write to the default shell for interpolating it, breaking it into separate command line arguments and then finally launching your command with these arguments. Or it can pass what you write to the shell your command (which it did launch without a shell - see the array-mode of system()) after it logs it with nothing interpolated. In this way you also get one huge command line parameter because the shell is not invoked in order to interpolate and break it on spaces. You see the shell does (irritating) interpolation but also breaks your parameters into an array to go to ARGV.

bw, bliako

EDIT: don't be scared by the "one huge command line parameter" I mentioned above, because Getopt::Long can parse it as a single string (as opposed to an ARGV-style array, which also does parse really beatifully).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11110073]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-25 02:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found