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

Like so?

my ($arg) = grep length, shift, 'default';
Or maybe so?
my $grzblgh = sub { length or $_ = shift for shift }; sub f { (my $arg = shift)->$grzblgh("default"); }

Replies are listed 'Best First'.
Re^2: Assigning default values to function arguments which may be “empty”
by flowdy (Scribe) on Aug 18, 2016 at 10:35 UTC

    Esp. syntax #2 looks a little overcomplicated, why not:

    sub f { length or $_ = "default value" for my $var = shift; do_something_with($var); }
    But in my humble opinion I'd prefer for the sake of clarity:
    sub f { my $var = shift; for ( $var ) { last if length; $_ = $DEFAULT; } ... }
Re^2: Assigning default values to function arguments which may be “empty”
by Wyrdweaver (Beadle) on Aug 19, 2016 at 05:33 UTC

    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; }

      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% --

        @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.

      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.