From: sln on
On Sun, 1 Nov 2009 15:32:17 -0000, "John" <john1949(a)yahoo.com> wrote:

>Hi
>
>The following can cause a warning if undefined
>
>my $value=$PAR{$name};
>
>What the best solution?
>
>my $value=$PAR{$name }or $value='';
>or
>my $value=''; if (defined $PAR{$name}) {$value=$PAR{$name}}
>or
>is there a cleaner solution?
>
>Regards
>John
>
>

The only uninitialized warning would be if $name were undefined
when you try to use it, ie: $PAR{$name}. It is always legitimate
to assing undef to a scalar variable.

Don't be confused between logical's 'or' and ||. They both do the
same logical test form, but 'or' is used for control flow.

So, my $value2 = 0 or 1; is functionally equivalent to
(my $value2 = 0) or 1; which assigns 0 to $value2 because
^^^^^^^^^^^^^^^
this expression is evaluated and the result of the assignment is always
*TRUE*, and because of that is always meaningless, a non-conditional.

You have to be carefull with || as well. Perl interprets 'undef' logically
equal to 0. So $value = undef || 1; and $value = 0 || 1; both assign
1 to $value.

Some examples follow below ..

-sln

---------
# 'or' can be used like '||' when used for control flow.

use strict;
use warnings;

my $value;

$value = undef || 1;
print $value,"\n";

$value = 0 || 1;
print $value,"\n\n";

# The ||, // and && operators return the last value evaluated
# (unlike C's || and &&, which return 0 or 1).
# --------------------
# For Perl 5.10 :
# $a // $b is exactly equivalent to defined($a) ? $a : $b

$value = defined(0) ? 0 : 3;
print $value,"\n";

$value = defined(undef) ? 0 : 3;
print $value,"\n";

$value = 0 // 3;
print $value,"\n";

$value = undef // 3;
print $value,"\n";

$value = undef;
my $value2 = 0 or 1;
print $value2,"\n";
__END__

Found = in conditional, should be == at gg.pl line 34.
1
1

0
3
0
3
0

From: Peter J. Holzer on
On 2009-11-01 18:43, sln(a)netherlands.com <sln(a)netherlands.com> wrote:
> On Sun, 1 Nov 2009 15:32:17 -0000, "John" <john1949(a)yahoo.com> wrote:
>>The following can cause a warning if undefined
>>
>>my $value=$PAR{$name};
>>
>>What the best solution?
>>
>>my $value=$PAR{$name }or $value='';
>>or
>>my $value=''; if (defined $PAR{$name}) {$value=$PAR{$name}}
>>or
>>is there a cleaner solution?
>
> The only uninitialized warning would be if $name were undefined
> when you try to use it, ie: $PAR{$name}. It is always legitimate
> to assing undef to a scalar variable.
>
> Don't be confused between logical's 'or' and ||. They both do the
> same logical test form, but 'or' is used for control flow.

Both 'or' and '||' have the same effect on control flow, i.e., both are
short-circuiting operators. The only difference is that 'or' has lower
precedence so sometimes you can avoid typing a pair of parentheses.

hp
From: Justin C on
On 2009-11-01, Andrew DeFaria <Andrew(a)DeFaria.com> wrote:
><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
> my $value ||= $PAR{$name}<br>

Why do you find it necessary to post over 100 lines of html just to
reply with one line? We understand your meaning by reading the text
that you post, not the code that your software generates. Please do
not post html here, plain-text only in this newsgroup.

Justin.

--
Justin C, by the sea.
From: sharma__r on
On Nov 1, 8:32 pm, "John" <john1...(a)yahoo.com> wrote:
> Hi
>
> The following can cause a warning  if undefined
>
> my $value=$PAR{$name};
>
> What the best solution?
>
> my $value=$PAR{$name }or $value='';
> or
> my $value=''; if (defined $PAR{$name}) {$value=$PAR{$name}}
> or
> is there a cleaner solution?
>

Not sure if it's a cleaner solution, but you could do this way:

##1
my $EMPTY_STR = q{};
my $value = ( !defined $name ) ? $EMPTY_STR # assign an empty
incase scalar $name not defined
: ( !exists $PAR{$name} ) ? $EMPTY_STR # assign an empty
incase key $name not found
: ( !defined $PAR{$name} ) ? $EMPTY_STR # assign an empty
incase key $name => value undef
: $PAR{$name}# lastly get the
defined value against the key $name
;

##2
my $value = q{} if !defined $name or !exists $PAR{$name} or !defined
$PAR{$name};
$value ||= $PAR{$name};

--Rakesh
From: Sherm Pendley on
Justin C <justin.0908(a)purestblue.com> writes:

> On 2009-11-01, Andrew DeFaria <Andrew(a)DeFaria.com> wrote:
>><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
>> my $value ||= $PAR{$name}<br>
>
> Why do you find it necessary to post over 100 lines of html just to
> reply with one line?

He knows he's being rude - he's been told that a thousand times. He
just doesn't care. My advice is, just killfile the a**hole and move on.

sherm--