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

I'm looking for an easy way to overwrite the empty string in a variable.

For undefined variables, I use the following:

$var //= '?';

Which means (to me at least), "If $var is undef, then assign it the value '?'".

I would like something similar for the case when $var contains '', e.g.

$var ///= '';

where I fabricated the '///=' operator for the purposes of illustration.

Currently, I use the following syntax:

$var = '?' if $var eq '';

Can anyone think of a tighter syntax? The above approach becomes cumbersome as variable names increase in length. (Hmm, as I write this, I'm considering that perhaps long variable names are the root cause of this issue. I do a lot of coding around SNMP, and so end up with extravaganzas like $dot3adAggPortActorOperState_before and dot3adAggPortActorOperState_after.)

Anyway, I'm wanting a clear way to represent the process of overwriting the empty string in variables -- if you have suggestions, plse let me know.

--sk

Replies are listed 'Best First'.
Re: searching for clear ways to overwrite the empty string
by Athanasius (Archbishop) on Apr 19, 2016 at 13:00 UTC

    The only strings that evaluate to false are '' (the empty string) and '0'. So, if you can afford to also overwrite the string when it comprises a single zero character, you can use logical-or:

    $var ||= '?';

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: searching for clear ways to overwrite the empty string
by haukex (Archbishop) on Apr 19, 2016 at 13:02 UTC

    Hi sk,

    The ||= operator will assign its right side to its left side if the left side is a "false" value (0, "0", "" and undef, as per Truth and Falsehood).

    use Data::Dumper; $Data::Dumper::Terse=1; $Data::Dumper::Useqq=1; $Data::Dumper::Indent=0; for my $x (0, "0", "", undef, "true", 1) { my $y=$x; $y||='?'; print Dumper($x), " -> ", Dumper($y), "\n"; } __END__ 0 -> "?" 0 -> "?" "" -> "?" undef -> "?" "true" -> "true" 1 -> 1

    Other than that, if you want to specifically test for the empty string, your best bet is probably length, as Your Mother showed. As of Perl v5.12, when you give it an undef input, it returns undef instead of issuing a warning, so you can just use length in your boolean test without worrying about definedness.

    Hope this helps,
    -- Hauke D

    Update 2019-08-17: Updated the link to "Truth and Falsehood".

Re: searching for clear ways to overwrite the empty string
by Your Mother (Archbishop) on Apr 19, 2016 at 12:49 UTC

    Maybe–

    # Might need 'no warnings "uninitialized";' to be quiet. $var = "?" unless length $var; # Or... :P $var = "?" unless defined $var and length $var;
Re: searching for clear ways to overwrite the empty string
by Eily (Monsignor) on Apr 19, 2016 at 13:03 UTC

    The empty string is one of the false values, so if your string can't be another false value (eg: "0"), $var ||= "?" will work (you'll have to use the // version first if you want a different behavior for undefined and false).

    The for keyword can also be used as an aliasing mechanism for your long name problem:
    $_ eq '' and $_ = '?' for $yourVariableWithAVeryLongName;
    This has the added benefit of making it easier to apply the same modification to several variables:
    $_ //= '?' for ($yourFirstVariableWithAVeryLongName, $yourSecondVariableWithAVeryLongName, $IShouldHaveUsedCopyPasteInsteadOfTyping);

Re: searching for clear ways to overwrite the empty string
by Anonymous Monk on Apr 19, 2016 at 17:08 UTC
      That's neat -- and it could be done like this to make sure that both "undef" and "empty string" are handled without warnings:
      $_ //= "?" or s/^\z/?/ for ( $this_variable, $that_variable, $another_ +variable );
      (UPDATE: But I'm inclined to think that when there's a problem with too many long variable names, it might mean that those variable names really ought to be keys in a hash.)

      (updated again to fix the mis-typed operator in the snippet.)

        Parens not needed.