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

when using $0 in my scripts, variable is replaced with whole invocation of the script with command line options and other parameters

No it's not. It's just the program name invoked.

$ cat a.pl #!/usr/bin/perl use feature qw( say ); say $0; $ pwd /home/ikegami $ perl a.pl foo a.pl $ perl ./a.pl foo ./a.pl $ perl /home/ikegami/a.pl foo /home/ikegami/a.pl $ perl "$HOME/a.pl" foo /home/ikegami/a.pl $ a.pl foo ./a.pl $ ./a.pl foo ./a.pl $ /home/ikegami/a.pl foo /home/ikegami/a.pl $ "$HOME/a.pl" foo /home/ikegami/a.pl

If you wanted to recreate the command, you could use

use String::ShellQuote qw( shell_quote ); my $cmd = shell_quote($0, @ARGV);
or
use String::ShellQuote qw( shell_quote ); my $cmd = shell_quote($^X, "--", $0, @ARGV);

Note that neither of these solutions will recreate options passed to perl rather than the script (such as -p, -C, etc.) Perl doesn't provide this information to the script.

Note that the command will be equivalent to the one used (subject to the aforementioned caveat), but it won't be a character for character match to what was used. For example, the exact quoting used may be different. There's no reliable way to get the command as entered because

Replies are listed 'Best First'.
Re^2: preserve quotes after $0 interpolation
by tobyink (Canon) on Dec 13, 2019 at 07:55 UTC

    Note that neither of these solutions will recreate options passed to perl rather than the script (such as -p, -C, etc.) Perl doesn't provide this information to the script.

    But see Devel::PL_origargv.

Re^2: preserve quotes after $0 interpolation
by afoken (Chancellor) on Dec 13, 2019 at 10:11 UTC
    use String::ShellQuote qw( shell_quote );

    The last two times I looked at String::ShellQuote (Re^2: Passing values from Perl script to shell script in 2009 and Re^4: quoting/escaping file names in 2014), it did not look good. The last change is from 2010. So, String::ShellQuote still has the same problems as in 2009, quoting myself:

    It works just for some unnamed version of some unnamed bourne shell. The author wanted to add more shells, but he did not since 2005. The test.t look very strange, especially I don't see any reasonable test for passing arguments via a shell. So, it's old, unmaintained, not well-tested, and broken for all shells except for that unspecified bourne shell. Use the multi-argument forms of exec, system, or open instead, they do not need quoting.

    See also The problem of "the" default shell.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      You seem to allege some problem with shell_quote, but none of the linked post identify one. The all seemed centered around the idea of avoiding the shell is better. While true, that's not a problem with shell_quote.

      What do you mean by "It did not look good". What problem does it have?

      The posts you link only rant the module about the fact the module only supports sh. Claiming that makes the module broken is ridiculous.

Re^2: preserve quotes after $0 + @ARGV interpolation
by richard.sharpe (Sexton) on Dec 13, 2019 at 12:35 UTC

    I'm sorry, my original question was not complete (fixed), of course there should be also @ARGV in question and actually I was doing that this way:

    my $tool_name = basename($0); my $tool_invocation = $tool_name." ".join(" ",@ARGV);

    The reason, why I am interested in such thing, is, that I am generating some configurations, with comments containing also how was it generated, to be able reproduce that generation just from comment inside it, just by copy & paste whole command from comment to shell. Restriction to particular shell type, e.g. bash, is no problem for me in my use case, therefore the idea with shell history fits my needs maybe the best.

    Thank you for tips!