in reply to Re^2: More comprehensive style guide for Perl docs than perlpodstyle?
in thread More comprehensive style guide for Perl docs than perlpodstyle?

An example of a Perl internal function with a poor interface is split, which has two optional arguments ...

Agreed. There is another function with two optional arguments which annoys me more, however, and that is substr. Even though I've read the documentation thousands of times I never quite believe that LENGTH is the optional argument and OFFSET is mandatory. Every time I go to use substr and know that it's "the wrong way round" it is just so illogical to me that I still have to pause and consult the docs again just to make sure.

  • Comment on Re^3: More comprehensive style guide for Perl docs than perlpodstyle?

Replies are listed 'Best First'.
Re^4: More comprehensive style guide for Perl docs than perlpodstyle?
by soonix (Chancellor) on Jan 07, 2019 at 10:25 UTC
    This one is easier to remember if you know of languages with functions like LEFT, RIGHT, and MID (or MIDSTR).
    And substr is the equivalent of MID, while you obviously see it as LEFT …
    # untested, as usual :-) and without the "replace" functionality sub midstr { return substr(shift, shift, shift); } sub leftstr { return substr(shift, 0, shift); } sub rightstr { return substr(shift, -shift); }
Re^4: More comprehensive style guide for Perl docs than perlpodstyle?
by eyepopslikeamosquito (Archbishop) on Jan 07, 2019 at 10:08 UTC

    As indicated at Re: Drunk on golf: 99 Bottles of Beer, there's another reason to dislike substr - it's way too long for golf! (compared to Python's slices). :) Also, I'm not a fan of using substr as an lvalue - it may be powerful but adds still more complexity and does my head in.

Re^4: More comprehensive style guide for Perl docs than perlpodstyle?
by soonix (Chancellor) on Jan 07, 2019 at 12:47 UTC
    And I just noted that the interface for substr looks suspiciously similiar to that of splice (LEFT → shift, RIGHT → pop)
Re^4: More comprehensive style guide for Perl docs than perlpodstyle?
by Veltro (Hermit) on Jan 07, 2019 at 10:52 UTC

    I would actually find that illogical. The function is called substr and not left (as soonix already indicates). So for me it is logic to provide a start and an end index. And personally I hate it when people create optional arguments somewhere in the middle of an argument list and so I love it how substr is defined.

      It is heartening to hear that you love it. OTOH, this just makes me sigh:

      perl -E 'say substr ("Hello World!", 5);'

        OTOH, then let's just build in a hash as eyepopslikeamosquito suggests:

        say substr ("Hello World!", { end-index => 5 } );

        If there is something that makes me sigh, then that is it. Say Good Bye to minimalistic code!