in reply to Uninitialized Value Question

Yes, it will assign the blank string. But the warning is for mentioning an undefined value in the if test itself. It is only a warning. You should try to quite the warning with something like this:
if ( defined $value_a and $value_a =~ /personal/ ) {...
Either that, or say something like this before the if:
$value_a ||= '';

Phil

Replies are listed 'Best First'.
Re^2: Uninitialized Value Question
by Zaxo (Archbishop) on Feb 27, 2007 at 18:22 UTC

    I'd write that initialization as:

    defined($value_a) or $value_a = '';
    The ||= op will convert zero to an empty string, which may provide surprises. There is the shiny new //= (I think it is) op to do that, but in the interest of portability I wouldn't use it yet.

    After Compline,
    Zaxo

      Fair enough, but I favor changing the if condition as in my first example. And, in the OP's code any value (including zero) was going to become the empty string when the regex failed.

      But the subtle problem you raise could well bite others. I'm also looing forward to //= installed most everywhere.

      Phil

Re^2: Uninitialized Value Question
by blazar (Canon) on Feb 28, 2007 at 10:36 UTC
    Yes, it will assign the blank string. But the warning is for mentioning an undefined value in the if test itself. It is only a warning. You should try to quite the warning with something like this:
    if ( defined $value_a and $value_a =~ /personal/ ) {...

    And one minor modification that no one seems to have mentioned yet would be to "factor" out the assignment with the ternary C<?:> operator, which also makes for a more concise but still fairly readable (actually, more readable IMHO) syntax like thus:

    $value_a = defined $value_a && $value_a =~ /personal/ ? 'checked' : '' +;

    Since in both cases $value_a would appear quite a number of times, and that makes the whole thing less readable, well at least for me, I feel like using a pronoun instead, that is a C<for> loop's aliasing property. In the second case this "trick" can easily be cast in the form of a statement modifier:

    $_ = defined && /personal/ ? 'checked' : '' for $value_a;

    But readability is such a subjective matter and this use so controversial that I won't certainly insist on it.