Login
You're viewing the techdon.dev public feed.
  • 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

Replies

  • 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