in reply to Re: Catch the exact command line including the quotation marks and so on
in thread Catch the exact command line including the quotation marks and so on

Hi, I don't get the desired behavior with ARGV. Here is an example:

perl CJ.pl -help -m "double quote goes here" 'single quote goes here'

if I do

my $script = join " ", $0, @ARGV; print "$script \n "; die;

I get:

CJ.pl -help -m doublequotegoeshere singlequotegoeshere

As you can see, not only quotes are gone, the spaces in the message is removed too! Here is the perl info I am using on Mac:

This is perl 5, version 16, subversion 3 (v5.16.3) built for darwin-th +read-multi-2level

Replies are listed 'Best First'.
Re^3: Catch the exact command line including the quotation marks and so on
by Your Mother (Archbishop) on Oct 23, 2015 at 20:14 UTC

    I don’t think so. You’re doing something else somewhere. And why the die? Might cloud order of ouput depending on what you do with the script.

    moo@cow~>perl ab -help -m "double quote goes here" 'single quote goes +here' ab -help -m double quote goes here single quote goes here Died at ab line 3. __END__ This is perl 5, version 18, subversion 2 (v5.18.2) built for darwin-2l +evel
      You are right. I was using Getopt::Declare without defer{}, and that was the result. die is there since I am testing, and I don't want the whole process to run. nothing to worry about!

      OK. I ended up doing the following. It is not exact but it helps me build meaningful command (based on recommendations on http://stackoverflow.com/questions/6156742/how-can-i-capture-the-complete-commandline-in-perl). Double quote and single quote are just replaced by double quote.

      my $cmd = `ps -o args $$ | grep CJ.pl`; my @cmd = split(/\s/,$cmd); my $cmdline = "$cmd[0] "." $cmd[1]"; foreach (@ARGV) { $cmdline .= /\s/ ? " \"" . $_ . "\"": " " . $_; } print "$cmdline\n";

      But I still think perl should have a better way of catching the command line. It is bizarre that you can't simply get it exact!

        Perl is called by the shell. The shell does preprocess the arguments (as you already observed), and hands only the already preprocessed arguments over to Perl, so Perl has no chance.

        You might try reading from ~/.bash_history, but that won't work with other shells…