unintialised warning

J

John

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
 
A

Andrew DeFaria

body { font: Helvetica, Arial, sans-serif; } p { font: Helvetica, Arial, sans-serif; } ..standout { font-family: verdana, arial, sans-serif; font-size: 12px; color: #993333; line-height: 13px; font-weight: bold; margin-bottom: 10px; } ..code { border-top: 1px solid #ddd; border-left: 1px solid #ddd; border-right: 2px solid #000; border-bottom: 2px solid #000; padding: 10px; margin-top: 5px; margin-left: 5%; margin-right: 5%; background: #ffffea; color: black; -moz-border-radius: 10px; } ..codedark { border-top: 10px solid #03f; border-left: 1px solid #ddd; border-right: 2px solid grey; border-bottom: 2px solid grey; padding: 10px; margin-top: 5px; margin-left: 5%; margin-right: 5%; background: black; color: yellow; -moz-border-radius: 10px; } #code { color: black; font-size: 14px; font-family: courier; padding-left: 5px; } #line-number { color: #804000; font-family: Arial; font-size: 14px; padding-right: 5px; border-right: 1px dotted #804000; } blockquote[type=cite] { padding: 0em .5em .5em .5em !important; border-right: 2px solid blue !important; border-left: 2px solid blue !important; } blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid maroon !important; border-left: 2px solid maroon !important; } blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid teal !important; border-left: 2px solid teal !important; } blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid purple !important; border-left: 2px solid purple !important; } blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid green !important; border-left: 2px solid green !important; } On 11/01/2009 08:32 AM, John 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?
my $value ||= $PAR{$name}
 
D

Dr.Ruud

John said:
The following can cause a warning if undefined

my $value=$PAR{$name};

my $value = defined($name) ? $PAR{$name} : undef;


This means that $value itself can still be undef.


Or did you mean 'name' in stead of $name?
 
J

John W. Krahn

Andrew said:
my $value ||= $PAR{$name}

That is exactly the same as saying:

my $value = $PAR{$name}

Because my() creates a variable that starts out undefined and an
undefined variable is false so the || test will always fail.


John
 
S

sln

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
 
P

Peter J. Holzer

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
 
J

Justin C

<!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.
 
S

sharma__r

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
 
J

Jürgen Exner

Justin C said:
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.

This has been discussed ad nauseum a while ago. Mr. DeFaria is resistant
to any attempt of reasoning and therefore ended up in most peoples
filters.
You must have missed that thread.

jue
 
A

Andrew DeFaria

body { font: Helvetica, Arial, sans-serif; } p { font: Helvetica, Arial, sans-serif; } ..standout { font-family: verdana, arial, sans-serif; font-size: 12px; color: #993333; line-height: 13px; font-weight: bold; margin-bottom: 10px; } ..code { border-top: 1px solid #ddd; border-left: 1px solid #ddd; border-right: 2px solid #000; border-bottom: 2px solid #000; padding: 10px; margin-top: 5px; margin-left: 5%; margin-right: 5%; background: #ffffea; color: black; -moz-border-radius: 10px; } ..codedark { border-top: 10px solid #03f; border-left: 1px solid #ddd; border-right: 2px solid grey; border-bottom: 2px solid grey; padding: 10px; margin-top: 5px; margin-left: 5%; margin-right: 5%; background: black; color: yellow; -moz-border-radius: 10px; } #code { color: black; font-size: 14px; font-family: courier; padding-left: 5px; } #line-number { color: #804000; font-family: Arial; font-size: 14px; padding-right: 5px; border-right: 1px dotted #804000; } blockquote[type=cite] { padding: 0em .5em .5em .5em !important; border-right: 2px solid blue !important; border-left: 2px solid blue !important; } blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid maroon !important; border-left: 2px solid maroon !important; } blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid teal !important; border-left: 2px solid teal !important; } blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid purple !important; border-left: 2px solid purple !important; } blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid green !important; border-left: 2px solid green !important; } a:link { color: blue; } a:visited { color: darkblue; } a:hover { color: black; background-color: #ffffcc; text-decoration: underline; } a:active { color: red; } On 11/03/2009 08:32 AM, Justin C wrote: On 2009-11-01, Andrew DeFaria &lt;[email protected]&gt; wrote:
&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;
my $value ||= $PAR{$name}&lt;br&gt;

Why do you find it necessary to post over 100 lines of html just to reply with one line? For much the same reason why you feel the need to complain about what other people do.
 
A

Andrew DeFaria

body { font: Helvetica, Arial, sans-serif; } p { font: Helvetica, Arial, sans-serif; } ..standout { font-family: verdana, arial, sans-serif; font-size: 12px; color: #993333; line-height: 13px; font-weight: bold; margin-bottom: 10px; } ..code { border-top: 1px solid #ddd; border-left: 1px solid #ddd; border-right: 2px solid #000; border-bottom: 2px solid #000; padding: 10px; margin-top: 5px; margin-left: 5%; margin-right: 5%; background: #ffffea; color: black; -moz-border-radius: 10px; } ..codedark { border-top: 10px solid #03f; border-left: 1px solid #ddd; border-right: 2px solid grey; border-bottom: 2px solid grey; padding: 10px; margin-top: 5px; margin-left: 5%; margin-right: 5%; background: black; color: yellow; -moz-border-radius: 10px; } #code { color: black; font-size: 14px; font-family: courier; padding-left: 5px; } #line-number { color: #804000; font-family: Arial; font-size: 14px; padding-right: 5px; border-right: 1px dotted #804000; } blockquote[type=cite] { padding: 0em .5em .5em .5em !important; border-right: 2px solid blue !important; border-left: 2px solid blue !important; } blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid maroon !important; border-left: 2px solid maroon !important; } blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid teal !important; border-left: 2px solid teal !important; } blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid purple !important; border-left: 2px solid purple !important; } blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid green !important; border-left: 2px solid green !important; } a:link { color: blue; } a:visited { color: darkblue; } a:hover { color: black; background-color: #ffffcc; text-decoration: underline; } a:active { color: red; } On 11/03/2009 11:44 AM, Sherm Pendley wrote:
Some *providers* even filter him, although I doubt it's personal; they are probably just filtering broken HTML-only articles that lack the correct MIME type header.
Everybody's free to do what they want.
I use x-privat.org, for example, and I didn't even know he was still posting here until now. Ignorance is indeed bliss in this case. :)
I haven't posted here in a while. Your assumption is incorrect as usual. Part of ignorance is the willful ignoring of the facts. You seem to have this down cold.
 
J

Justin C

This has been discussed ad nauseum a while ago. Mr. DeFaria is resistant
to any attempt of reasoning and therefore ended up in most peoples
filters.
You must have missed that thread.

Yes, I must have done. I shall make a note and not chastise the deviant
again.

Justin.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top