Login
You're viewing the oslo.town public feed.
  • May 5, 2026, 4:56 PM

    Thing that just bit me because #Perl is not quite timtoady sometimes:

    ```
    my $foo = pop split(/bla/, $str);
    ```

    (can't pop scalar in modern Perl and this construct puts split into scalar context I guess and that's not fun for pop)

    (wanted to avoid the ugly `(...)[-1]` construct)

    💬 2🔄 0⭐ 0

Replies

  • 💬 1🔄 0⭐ 0
  • May 5, 2026, 5:51 PM

    @ology I'd expect square brackets to, though, or in postderef form `[...]->@*` but that's also kinda ugly and potentially more work than just grabbing the last item

    💬 1🔄 0⭐ 0
  • May 5, 2026, 6:00 PM

    @phl From an acquaintance: "@{ split ... } evaluates split in scalar context, then dereferences the result ... unfortunately split returns a count in scalar context, not an arrayref"

    💬 1🔄 0⭐ 0
  • May 5, 2026, 6:03 PM

    @phl And another aquaintance said this, which works fine:

    `$foo = pop @{ [ split /bla/, $str ] }`

    :)

    💬 1🔄 0⭐ 0
  • 💬 0🔄 0⭐ 0
  • May 6, 2026, 10:46 AM

    @phl I wonder if that error message is an issue. I wouldn't expect it to work anyway, because pop really expects an array, as it has to modify it. I don't think it can do that with the list split generates.

    That is, it seems more in the category of "Can't modify constant item" than "Experimental pop on scalar" ... and I don't think it's the context.

    1> pop split /bla/, $str
    Experimental pop on scalar is now forbidden

    💬 1🔄 0⭐ 0
  • May 6, 2026, 10:49 AM

    @fuzzix Good point. I guess the can't modify constant item one would be more appropriate (although also somewhat confusing :D)

    And yeah, here's me confusing lists with arrays 25+ years into the Perl world still :D It's I guess a bit of a footgun.

    💬 1🔄 0⭐ 0
  • May 6, 2026, 11:34 AM

    @phl Just tried out perl 5.12 (the version before experimental "Array and hash container functions accept references") and got a much more helpful message:

    $ perl -E 'my $str="fooblabar"; pop split(/bla/, $str);'
    Type of arg 1 to pop must be array (not split) ...

    💬 1🔄 0⭐ 0
  • 💬 0🔄 0⭐ 0
  • May 7, 2026, 8:37 PM

    @phl yep, popping a scalar was found to be too dwimmy. in which case one might expect pop to provide a list context but it's not _that_ special, alas.

    💬 1🔄 0⭐ 0
  • 💬 0🔄 0⭐ 0