in reply to Re^2: Empty pattern in regex
in thread Empty pattern in regex

I tried "m//" as reported on your 'next' expression and got 'd e f g h', as expected. From my perlop on 5.36.

    The empty pattern "//"
            If the *PATTERN* evaluates to the empty string, the last
            *successfully* matched regular expression is used instead. In
            this case, only the "g" and "c" flags on the empty pattern are
            honored; the other flags are taken from the original pattern. If
            no match has previously succeeded, this will (silently) act
            instead as a genuine empty pattern (which will always match).

            Note that it's possible to confuse Perl into thinking "//" (the
            empty regex) is really "//" (the defined-or operator). Perl is
            usually pretty good about this, but some pathological cases
            might trigger this, such as "$x///" (is that "($x) / (//)" or
            "$x // /"?) and "print $fh //" ("print $fh(//" or
            "print($fh //"?). In all of these examples, Perl will assume you
            meant defined-or. If you meant the empty regex, just use
            parentheses or spaces to disambiguate, or even prefix the empty
            regex with an "m" (so "//" becomes "m//").

Replies are listed 'Best First'.
Re^4: Empty pattern in regex
by choroba (Cardinal) on Oct 19, 2023 at 13:23 UTC
    so "//" becomes "m//"

    The double quotes are part of the text, not part of the code (as you can infer from them being already around the //).

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      Of course, you are probably right, but why did this work?

        % perl -le 'print for a .. z' | perl  -nle 'if (/d/ .. /h/) { next unless "m//"; print }'
        d
        e
        f
        g
        h
      

      Is that just interpolation at work? Do we have to read the source code to understand what is going on here, or is supposed to be going on? Talk about ambiguity... I tried //; m//; and "m//;" on that Foy code I reported and none yielded a successful match. Every example reported:

      No nothing
      $1: 
      $2: 
      $3: 
      $&: Perl
      

      the value from the previous successful match.

        > but why did this work?

        next unless 1; never runs next, as 1 is always true. So is "m//" or any other non-empty string different to 0.

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]