in reply to Recursive subroutines with sort

sort can take a subroutine name as its first argument. The subroutine performs the comparison and returns the negative, positive or zero result. The delta you've linked to says that as of 5.10.0 such subroutines may now be recursive.

Replies are listed 'Best First'.
Re^2: Recursive subroutines with sort
by choroba (Cardinal) on Jul 08, 2019 at 10:52 UTC
    I've investigated the deltas because of Syntax::Construct and it seemed it had been possible to use recursive subroutines in older Perls, too. 5.10 was just the version that documented the fact.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re^2: Recursive subroutines with sort
by holli (Abbot) on Jul 08, 2019 at 10:16 UTC
    Hah. I didn't know that. I have exclusively used the BLOCK form to this date. It's nice being able to refer to a sub by name, but when is it useful that sub being able to recurse?


    holli

    You can lead your users to water, but alas, you cannot drown them.
      > but when is it useful that sub being able to recurse?

      what comes to mind are chained comparisons in sort °

      sort { $a->[0] cmp $b->[0] or $a->[1] cmp $b->[1] # * or $a->[2] cmp $b->[2] # * or ... # * } @AoA # *) only check if former was equal == 0 == false

      if you don't know the number of fields, recursive comparisons come in handy.

      I'm bu-la-zy, so constructing the recursion is left as exercise. ;-)

      HTH!

      update

      In hindsight ... this particular case could also be solved in a loop.

      So think about a more complicated data structures like trees to be sorted.

      In the end it's about convenience ... every loop can be expressed as recursion and vice versa.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      °) see examples in code from line 39 onwards