Login
You're viewing the techdon.dev public feed.
  • Sep 17, 2025, 11:29 AM

    Hey, it's time to embarrass myself again! Here's a #perl question/musing.

    So we have /PATTERN/ which is m/PATTERN/ which is "match $_ (or the content of the variable specified by the =~ operator) against PATTERN".

    We also have qr/STRING/ which is "make a regex PATTERN out of STRING".

    As such, passing a // (m//) "call" as argument to things like split feels ever so slightly odd as we're not actually matching at that moment, but just providing a regex pattern split should match against. Right?

    💬 1🔄 0⭐ 0

Replies

  • Sep 17, 2025, 12:21 PM

    @phl
    split() does indeed allow you to split a string by a pattern. It will return a list of values from the string that were separated by that pattern. So you are effectively getting the pieces that *don't* match the pattern in the returned list, because the pattern was what the string was split on.

    split() also accepts a string as a separator, if you don't need the complication of a pattern.

    💬 0🔄 0⭐ 0
  • Sep 17, 2025, 12:44 PM

    @traecer I know I can supply those and what split does, that wasn't the point I was trying to make.

    The musing was about how we aren't supplying a pattern, but a call to a match operation (which masquerades as a pattern, to be used internally by the function).

    i.e. pseudocode for my sudden discomfort with the syntax after decades:
    ```
    split(match($_, PATTERN), $str)
    ```
    instead of just the pattern itself :)

    💬 0🔄 0⭐ 0
  • Sep 17, 2025, 12:53 PM

    @phl @traecer

    so m/PATTERN/ doesn't actually do matching, it's just a short hand that expands (in certain contexts) to $_ =~ m/PATTERN/. The =~ part does the actual regex matching. m/PATTERN/ is just a regex value, thus you are actually passing a value to split which does the matching itself rather than the results of a function call.

    Also while you can pass a string to split, it just gets interpreted as a regex anyway.

    💬 0🔄 0⭐ 0
  • 💬 1🔄 0⭐ 0
  • Sep 17, 2025, 1:19 PM

    @barubary @phl @traecer

    I was less precise than I could have been, I suppose. =~ is what *causes* the regex matching to happen. m/PATTERN/ on it's own does not. =~ is also used for both transliterations and substitutions.

    💬 1🔄 0⭐ 0
  • Sep 17, 2025, 1:39 PM

    @splatt9990 @phl @traecer The problem I have with

    m/PATTERN/ doesn't actually do matching, it's just a short hand that expands (in certain contexts) to $_ =~ m/PATTERN/

    is that m/PATTERN/ expands to $_ =~ m/PATTERN/ in literally every context except for the specific case of split's first argument (or if it's already the right-hand operand of a =~, of course).

    (Also, it's not what the implementation actually does. m/.../ on its own produces a "match" op without an implicitly added reference to $_ anywhere. That's why e.g. B::Deparse produces different output for /foo/ vs $_ =~ /foo/.)

    💬 1🔄 0⭐ 1
  • Sep 17, 2025, 1:06 PM

    @phl Right. The reason for this weirdness is history. split is older than qr (regex object). In fact, split was added to Perl before it had objects or references, so the syntax is a bit odd. (There are more weird special cases: If you pass /^/, it behaves like /^/m instead, and the empty match op m// normally means "use the last successfully matched regex again", but in split it means "match between all characters in the string", i.e. return a list of single characters.)

    A similar thing happened with sort (and map and grep), which logically should take functions (i.e. code references), but they were designed before Perl had code references or anonymous subs, so they take ad-hoc blocks instead (or unevaluated expressions in the case of map/grep).

    💬 1🔄 0⭐ 0
  • Sep 17, 2025, 1:19 PM

    @barubary

    ```
    ~$ perl -E 'use Data::Printer;
    sub schplitt ($pat, $str) {
    p $pat; p $str;
    };
    my @foo = schplitt(/:/, "user:pass");'

    Too few arguments for subroutine 'main::schplitt' (got 1; expected 2) at -e line 5.
    ```

    That's also certainly an... interesting error

    ```
    my @foo = schplitt(qr/:/, "user:pass");'

    : (modifiers: u)
    "user:pass"
    ```

    works of course as expected, is just rather more unsightly on the caller side than the builtin because of the qr prefix adding some noise.

    💬 1🔄 0⭐ 0
  • Sep 17, 2025, 1:31 PM

    @phl Yeah, schplitt(/:/, "user:pass") matches $_ (which is undefined, but you didn't enable warnings) against /:/ in list context, so the failing match returns an empty list, which is concatenated with "user:pass", giving a single-element argument list.

    💬 1🔄 0⭐ 0