in reply to Re: Assigning default values to function arguments which may be “empty”
in thread Assigning default values to function arguments which may be “empty”

I really like this answer. It prompted me to think more widely about the issue, looking at functions (built-ins and CORE modules) that might be helpful. This led to the construction that I'm now favoring:

sub f { # ( [$:arg] ) use 5.007003; # perl v5.7.3; required for List::Util to be availab +le in CORE use List::Util qw/ first /; my $arg = first { defined && length } ( shift, 'default_value' ); $arg; }
  • Comment on Re^2: Assigning default values to function arguments which may be “empty”
  • Download Code

Replies are listed 'Best First'.
Re^3: Assigning default values to function arguments which may be �empty�
by BrowserUk (Patriarch) on Aug 19, 2016 at 09:22 UTC

    Why would you add a completely unnecessary dependency to do something more clearly and simply done using the base language; and pay a 100%+ performance penalty for the privilege?

    #! perl -slw use strict; use Benchmark qw[ cmpthese ]; sub f1 { # ( [$:arg] ) use 5.007003; # perl v5.7.3; required for List::Util to be availab +le in CORE use List::Util qw/ first /; my $arg = first { defined && length } ( shift, 'default_value' ); $arg; } sub f2 { my $arg = defined $_[0] && length $_[0] ? shift : 'default'; $arg; } our @args = ( undef, '', 'fred', 1, 1.5 ) x 1000; cmpthese -1, { stupid => q[ f1( $_ ) for @args ], simple => q[ f2( $_ ) for @args ], } __END__ C:\test>junk0 Rate stupid simple stupid 130/s -- -56% simple 294/s 127% --

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
      a) are the -s and -l switches necessary?
      b) for completeness' sake:
      sub f0 { my ($arg) = grep length, shift, 'default'; $arg; }
      gave me
      Rate stupid golf simple stupid 250/s -- -13% -39% golf 286/s 15% -- -30% simple 411/s 65% 44% --
        a) are the -s and -l switches necessary?

        They are in every script I write, unless the function of the script requires their removal to operate properly; which is a very rare occurrence.

        1. The -l so I don't have to litter my code with  . "\n"; or equivalent.
        2. The -s so that if I want to add some simple parameterisations. Eg.
          #! perl -slw use strict; use Benchmark qw[ cmpthese ]; sub f1 { # ( [$:arg] ) use 5.007003; # perl v5.7.3; required for List::Util to be availab +le in CORE use List::Util qw/ first /; my $arg = first { defined && length } ( shift, 'default_value' ); $arg; } sub f2 { my $arg = defined $_[0] && length $_[0] ? shift : 'default'; $arg; } our $ITERS //= 1000; our @args = ( undef, '', 'fred', 1, 1.5 ) x $ITERS; cmpthese -1, { stupid => q[ f1( $_ ) for @args ], simple => q[ f2( $_ ) for @args ], } __END__ C:\test>junk0 -ITERS=10000 Rate stupid simple stupid 12.8/s -- -58% simple 30.2/s 136% --

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
        In the absence of evidence, opinion is indistinguishable from prejudice.

        You are doing enough work as you have missed the test for defined'ness.

      @BrowserUk,

      Well, from your choice of function names, I see you have a strong negative opinion about the choice. I, respectfully, disagree.

      Firstly, your f2() solution is great and on target. But, I'm a language polyglot, using perl to a moderate extent... not an expert, by far. And to me, the f1() version using List::Util is easier to read and understand at a glance. I also like that it doesn't have an extra visible temporary variable, not for performance reasons, but to minimize the chance for error when reusing the idiom.

      Your performance argument, while well proven and valid from the viewpoint of that single line, doesn't hold up when looking at the use of the function or script as a whole. It's a premature optimization. There is literally no perceptible (or even measurable) difference in the script execution performance between any of the solutions presented. It's fun to analyze and debate. But, until I see some sort of real performance bottleneck by the function using this idiom during some actual use, I'm leaning to the side of easier comprehension (again, for me).

      But, thank you, for applying your brain power to this... as always, I enjoy reading and learning from your comments.

Re^3: Assigning default values to function arguments which may be “empty”
by hippo (Archbishop) on Aug 19, 2016 at 08:58 UTC
    use 5.007003; # perl v5.7.3; required for List::Util to be available in CORE

    I must be missing something but I fail to see the advantage of this. You are deliberately disallowing users with perl 5.7.0 (say) the opportunity to run this script even if they have List::Util installed. Why would you do that?

      It was included to guarantee that there would be no error from a missing dependency. I take your point though, and will re-think how to express it with more flexibility for the case where the module is already installed.

      Thanks for the comment.

        There are actually 2 problems with this approach. The first is the one I mentioned above where a perhaps pointless error is thrown for users with older perls. The second is that just because a module is in core for a certain perl version does not guarantee that it is installed. RHEL does this a lot, for example: if I were to run your code on a stock RHEL5 machine it would pass the version test (5.8.8) but then throw the compiler error because the module isn't actually installed.

        FWIW, I think it would be considered fine to use a module which has been in core that long anyway. Folks running really old perls or those without core modules present will mostly be used to installing such dependencies. Your choice, of course.