• Re: perl test

    From Retro Guy@21:1/5 to Peter Dean on Sat Sep 7 05:07:13 2024
    XPost: misc.test

    On Sat, 7 Sep 2024 03:01:14 -0000 (UTC), Peter Dean wrote:

    In comp.lang.misc Retro Guy <retroguy@novabbs.com> wrote:
    On Fri, 6 Sep 2024 2:14:33 +0000, Lawrence D'Oliveiro wrote:

    On Sat, 31 Aug 2024 12:33:39 +0000, Retro Guy wrote:

    I figured out my Perl issue. =~ s/([\"])/\\$1/g; does the trick.

    If that Perl code does what I think it does, the following Python
    equivalent is simpler:

    ½str-expr╗.replace('"', '""')

    The Perl code above escapes quotes, so adds '\' before any "

    I know nothing of Python :)

    I actually ended up with =~ s/([\$"])/\\$1/g; in my final code. I needed
    to escape both quotes and '$'


    would it hurt to backslash everything nonalphanumeric?

    perldoc -f quotemeta

    My use case was escaping a set of strings for use in a command line.
    Something like:

    $arguments = '"' . $tempfile . '" "' . $name . '" "' . $something . '"'; $returnvalue = `/usr/bin/php /path/to/program.php $arguments`;

    I needed to escape " and $, but anything else and the '\' would remain,
    causing the value of the string to be incorrect when used at the target.

    There's probably a better way in Perl than what I did, but I don't know
    much about Perl. I do understand regex reasonably well, but not Perl as a language.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Peter Dean@21:1/5 to Retro Guy on Sat Sep 7 14:30:03 2024
    XPost: misc.test

    In comp.lang.misc Retro Guy <retroguy@novabbs.com> wrote:
    On Sat, 7 Sep 2024 03:01:14 -0000 (UTC), Peter Dean wrote:

    In comp.lang.misc Retro Guy <retroguy@novabbs.com> wrote:
    On Fri, 6 Sep 2024 2:14:33 +0000, Lawrence D'Oliveiro wrote:

    On Sat, 31 Aug 2024 12:33:39 +0000, Retro Guy wrote:

    I figured out my Perl issue. =~ s/([\"])/\\$1/g; does the trick.

    If that Perl code does what I think it does, the following Python
    equivalent is simpler:

    «str-expr».replace('"', '""')

    The Perl code above escapes quotes, so adds '\' before any "

    I know nothing of Python :)

    I actually ended up with =~ s/([\$"])/\\$1/g; in my final code. I needed >>> to escape both quotes and '$'


    would it hurt to backslash everything nonalphanumeric?

    perldoc -f quotemeta

    My use case was escaping a set of strings for use in a command line. Something like:

    $arguments = '"' . $tempfile . '" "' . $name . '" "' . $something . '"'; $returnvalue = `/usr/bin/php /path/to/program.php $arguments`;

    I needed to escape " and $, but anything else and the '\' would remain, causing the value of the string to be incorrect when used at the target.

    There's probably a better way in Perl than what I did, but I don't know
    much about Perl. I do understand regex reasonably well, but not Perl as a language.

    Your way looks good. Nearly identical to solution in chapter 1.18 of "Perl Cookbook"
    by Tom Christiansen.

    $var =~ s/([CHARLIST])/\\$1/g;

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Retro Guy@21:1/5 to Lawrence D'Oliveiro on Sat Sep 7 23:11:41 2024
    XPost: misc.test

    On Sat, 7 Sep 2024 22:59:41 +0000, Lawrence D'Oliveiro wrote:

    On Sat, 7 Sep 2024 05:07:13 -0700, Retro Guy wrote:

    My use case was escaping a set of strings for use in a command line.

    If you want to pass arguments to an external program, don’t bother going through a shell if you can help it. Invoke the program and pass the command-line arguments directly, then you don’t have to worry about
    shell
    specials.

    That would be my preference, but I'm not sure how to do that from Perl.

    My use case is internal (not code expected to run anywhere else), so
    this works, but I should put some time into learning how to invoke the
    php program from Perl properly when I have some time.

    --
    Retro Guy

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lawrence D'Oliveiro@21:1/5 to Retro Guy on Sat Sep 7 22:59:41 2024
    XPost: misc.test

    On Sat, 7 Sep 2024 05:07:13 -0700, Retro Guy wrote:

    My use case was escaping a set of strings for use in a command line.

    If you want to pass arguments to an external program, don’t bother going through a shell if you can help it. Invoke the program and pass the command-line arguments directly, then you don’t have to worry about shell specials.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lawrence D'Oliveiro@21:1/5 to Retro Guy on Sun Sep 8 06:38:17 2024
    XPost: misc.test

    On Sat, 7 Sep 2024 23:11:41 +0000, Retro Guy wrote:

    On Sat, 7 Sep 2024 22:59:41 +0000, Lawrence D'Oliveiro wrote:

    If you want to pass arguments to an external program, don’t bother
    going through a shell if you can help it. Invoke the program and pass
    the command-line arguments directly, then you don’t have to worry about
    shell specials.

    That would be my preference, but I'm not sure how to do that from Perl.

    From the perlfunc(1) man page:

    system LIST
    system PROGRAM LIST
    Does exactly the same thing as "exec", except that a fork
    is done first and the parent process waits for the child
    process to exit. Note that argument processing varies
    depending on the number of arguments. If there is more than
    one argument in LIST, or if LIST is an array with more than
    one value, starts the program given by the first element of
    the list with arguments given by the rest of the list. If
    there is only one scalar argument, the argument is checked
    for shell metacharacters, and if there are any, the entire
    argument is passed to the system's command shell for
    parsing (this is "/bin/sh -c" on Unix platforms, but varies
    on other platforms). If there are no shell metacharacters
    in the argument, it is split into words and passed directly
    to "execvp", which is more efficient.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Retro Guy@21:1/5 to Lawrence D'Oliveiro on Sun Sep 8 11:46:44 2024
    XPost: misc.test

    On Sun, 8 Sep 2024 6:38:17 +0000, Lawrence D'Oliveiro wrote:

    On Sat, 7 Sep 2024 23:11:41 +0000, Retro Guy wrote:

    On Sat, 7 Sep 2024 22:59:41 +0000, Lawrence D'Oliveiro wrote:

    If you want to pass arguments to an external program, don’t bother
    going through a shell if you can help it. Invoke the program and pass
    the command-line arguments directly, then you don’t have to worry about >>> shell specials.

    That would be my preference, but I'm not sure how to do that from Perl.

    From the perlfunc(1) man page:

    system LIST
    system PROGRAM LIST
    <snip>

    Thank you for the info, I've now done some reading about system, exec,
    etc. and yes, system looks to be the correct choice.

    I'm basically calling a php script and need to wait for it to complete,
    then deal with whether a file exists once back in the perl script. This
    is all working perfectly fine for me right now, but no reason I can't
    improve the method used (do it properly) at some point. I'm sure this
    can all be done in Perl (no need for PHP), but I'm able to slap out PHP
    code quickly, so it's my first choice.

    Thanks again for your input.

    --
    Retro Guy

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lawrence D'Oliveiro@21:1/5 to Retro Guy on Fri Sep 6 02:14:33 2024
    XPost: misc.test

    On Sat, 31 Aug 2024 12:33:39 +0000, Retro Guy wrote:

    I figured out my Perl issue. =~ s/([\"])/\\$1/g; does the trick.

    If that Perl code does what I think it does, the following Python
    equivalent is simpler:

    «str-expr».replace('"', '""')

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Retro Guy@21:1/5 to Lawrence D'Oliveiro on Fri Sep 6 12:06:32 2024
    XPost: misc.test

    On Fri, 6 Sep 2024 2:14:33 +0000, Lawrence D'Oliveiro wrote:

    On Sat, 31 Aug 2024 12:33:39 +0000, Retro Guy wrote:

    I figured out my Perl issue. =~ s/([\"])/\\$1/g; does the trick.

    If that Perl code does what I think it does, the following Python
    equivalent is simpler:

    «str-expr».replace('"', '""')

    The Perl code above escapes quotes, so adds '\' before any "

    I know nothing of Python :)

    I actually ended up with =~ s/([\$"])/\\$1/g; in my final code. I needed
    to escape both quotes and '$'

    --
    Retro Guy

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Retro Guy@21:1/5 to Lawrence D'Oliveiro on Fri Sep 6 16:51:15 2024
    XPost: misc.test

    On Fri, 6 Sep 2024 23:38:08 -0000 (UTC), Lawrence D'Oliveiro wrote:

    On Fri, 6 Sep 2024 12:06:32 +0000, Retro Guy wrote:

    On Fri, 6 Sep 2024 2:14:33 +0000, Lawrence D'Oliveiro wrote:

    ½str-expr╗.replace('"', '""')

    The Perl code above escapes quotes, so adds '\' before any "

    All right, then:

    ½str-expr╗.replace('"', '\\"')

    That's readable and makes sense.

    Now I'm back to php where I somewhat know what I'm doing :)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lawrence D'Oliveiro@21:1/5 to Retro Guy on Fri Sep 6 23:38:08 2024
    XPost: misc.test

    On Fri, 6 Sep 2024 12:06:32 +0000, Retro Guy wrote:

    On Fri, 6 Sep 2024 2:14:33 +0000, Lawrence D'Oliveiro wrote:

    «str-expr».replace('"', '""')

    The Perl code above escapes quotes, so adds '\' before any "

    All right, then:

    «str-expr».replace('"', '\\"')

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lawrence D'Oliveiro@21:1/5 to Retro Guy on Sat Sep 7 00:46:41 2024
    XPost: misc.test

    On Fri, 6 Sep 2024 16:51:15 -0700, Retro Guy wrote:

    On Fri, 6 Sep 2024 23:38:08 -0000 (UTC), Lawrence D'Oliveiro wrote:

    «str-expr».replace('"', '\\"')

    That's readable and makes sense.

    Can also be

    «str-expr».replace('"', r'\"')

    Now I'm back to php where I somewhat know what I'm doing :)

    Aagh.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Peter Dean@21:1/5 to Retro Guy on Sat Sep 7 03:01:14 2024
    XPost: misc.test

    In comp.lang.misc Retro Guy <retroguy@novabbs.com> wrote:
    On Fri, 6 Sep 2024 2:14:33 +0000, Lawrence D'Oliveiro wrote:

    On Sat, 31 Aug 2024 12:33:39 +0000, Retro Guy wrote:

    I figured out my Perl issue. =~ s/([\"])/\\$1/g; does the trick.

    If that Perl code does what I think it does, the following Python
    equivalent is simpler:

    «str-expr».replace('"', '""')

    The Perl code above escapes quotes, so adds '\' before any "

    I know nothing of Python :)

    I actually ended up with =~ s/([\$"])/\\$1/g; in my final code. I needed
    to escape both quotes and '$'


    would it hurt to backslash everything nonalphanumeric?

    perldoc -f quotemeta

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lawrence D'Oliveiro@21:1/5 to Peter Dean on Sat Sep 7 08:11:38 2024
    XPost: misc.test

    On Sat, 7 Sep 2024 03:01:14 -0000 (UTC), Peter Dean wrote:

    would it hurt to backslash everything nonalphanumeric?

    Does Perl have the equivalent of Python’s re.escape <https://docs.python.org/3/library/re.html#re.escape>?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Peter Dean@21:1/5 to Lawrence D'Oliveiro on Sat Sep 7 08:19:37 2024
    XPost: misc.test

    In comp.lang.misc Lawrence D'Oliveiro <ldo@nz.invalid> wrote:
    On Sat, 7 Sep 2024 03:01:14 -0000 (UTC), Peter Dean wrote:

    would it hurt to backslash everything nonalphanumeric?

    Does Perl have the equivalent of Python’s re.escape <https://docs.python.org/3/library/re.html#re.escape>?

    perldoc -f quotemeta

    I thought I already said that. oops.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Peter Dean@21:1/5 to Peter Dean on Sat Sep 7 09:18:53 2024
    XPost: misc.test

    In comp.lang.misc Peter Dean <p.dean@invalid.net> wrote:
    In comp.lang.misc Lawrence D'Oliveiro <ldo@nz.invalid> wrote:
    On Sat, 7 Sep 2024 03:01:14 -0000 (UTC), Peter Dean wrote:

    would it hurt to backslash everything nonalphanumeric?

    Does Perl have the equivalent of Python’s re.escape
    <https://docs.python.org/3/library/re.html#re.escape>?

    perldoc -f quotemeta

    I thought I already said that. oops.

    Actually reread it myself and realised it won't do the right thing with $. Ignore me.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)