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

You can also used defined() directly:

use warnings; use strict; print f() ."\n"; print f('hi') ."\n"; sub f { my $arg = defined $_[0] ? shift : 'default'; }

output:

default hi

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

    That doesn't cover his case for the empty string to also be defaulted; so:

    sub f { my $arg = defined $_[0] && length $_[0] ? shift : 'default'; }

    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.

      I'm experimenting with some variations based on ideas here and elsewhere (BTW, thanks for being so much friendlier than codereview.stackexchange.com! OMG, they are tightly wound).

      Thanks, again!

      d'oh! Yeah, I completely missed the whole empty thing.