nsupathy0 has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks,
I have a sub and I am passing a scalar into it. I want to set the receiving variable to null string if there is no incoming variable. What's the best, smartest way to do it. Thanks for your kind wisdom.
sub mycheck { # the following throws a warning my $db = $_ or my $db = ""; }

Replies are listed 'Best First'.
Re: passing a scalar to a function
by moritz (Cardinal) on May 19, 2008 at 10:48 UTC
    Arguments to subroutines are passed in the array @_, not in $_.

    You could do something like this:

    sub mycheck { my $db = shift || ""; } # or sub mycheck2 { my $db = @_ ? $_[0] : ""; }

      I want to point out, that we should be aware of the difference in those two solutions:

      The first solution will set $db to "", when the argument evaluates to FALSE.
      This will happen, when the argument is undef, empty string or 0 [zero]).

      So if undef and zero are wanted and valid values, the second solution should be used.

      Thank you for your wisdom. That was really helpful. $_ was a typo. I thought there was a "perl" way of doing this. The idiom  @_ ? $_[0] : "" was a better one.
Re: passing a scalar to a function
by Corion (Patriarch) on May 19, 2008 at 10:49 UTC

    You can't do it the way you do. It would have helped if you showed us the warning you got too.

    You seem to want to pass around parameters in the global $_ variable instead of using the common way of passing parameters in the function call, which populates @_. I guess that that's just a typo by you.

    The best approach to check for parameters is the following:

    sub mycheck { if (@_ == 0) { # No parameters passed, assume default @_ = (''); }; my ($db) = @_; ... };

    ... but you could also set up some other ways, depending on how your default works:

    sub mycheck { my ($db) = @_; $db = "" unless defined $db; # will always map undef to '' ... };
Re: passing a scalar to a function
by dragonchild (Archbishop) on May 19, 2008 at 13:41 UTC
    There is no such thing as the null string. It is the empty string. This is not just being pedantic. NULL has a number of other meanings, particularly in C (where it means '\0') and SQL (where is means "unknown value").

    As for your question, I generally do:

    sub function1 { my ($arg1, $arg2) = @_; $arg1 = "" unless defined $arg1; }
    If you're using 5.10 or higher, you can do:
    sub function1 { my ($arg1, $arg2) = @_; $arg1 //= ""; # This is the new defined-or operator. }

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: passing a scalar to a function
by Narveson (Chaplain) on May 19, 2008 at 11:07 UTC

    Useful habit: when you type

    sub mycheck {

    go on and type

    my () = @_;

    right away. In the present case you would fill in the list of receiving variables with $db:

    my ($db) = @_;

    Since you want the default value to be the null string, you can go on and say

    $db ||= "";
Re: passing a scalar to a function
by psini (Deacon) on May 19, 2008 at 10:48 UTC

    mycheck(); or perhaps I've not understand your question?

    Revised Ok, I didn't understand the question :)

    Rule One: Do not act incautiously when confronting a little bald wrinkly smiling man.