uninitialized value in concatenation ...

C

Chris Conwell

I've been coding in Perl for some years now but here's a snippet of
code that's got me really stumped:

--------------------------------------------------------------------------------
Code
--------------------------------------------------------------------------------

3913 sub print_price_nicely {
3914 my ($model,$link,$price) = (@_);
3915 return unless ($price);
3916 if ($price) {
3917 if ($price eq "") {

--------------------------------------------------------------------------------

This is giving the error:

"Use of uninitialized value in concatenation (.) or string at
cgi/phones.pl line 3917."


How can the script reach line 3917 and then report "uninitialized
value"?!?!?

Thanks in advance,


Chris
 
T

Tore Aursand

3913 sub print_price_nicely {
3914 my ($model,$link,$price) = (@_);
3915 return unless ($price);
3916 if ($price) {
3917 if ($price eq "") {

This is giving the error:

"Use of uninitialized value in concatenation (.) or string at
cgi/phones.pl line 3917."

Give us the lines in the scope created on line 3917. One other thing is
that you don't need to check 'if ( $price )' and/or 'if ( $price eq "" )'
as long as you do 'return unless ( $price )'.

(But please remember that '$price' with a value of 0 also will be skipped,
even though I recall extremely cheap is also a price to pay...)

:)
 
A

Anno Siegel

Chris Conwell said:
I've been coding in Perl for some years now but here's a snippet of
code that's got me really stumped:

--------------------------------------------------------------------------------
Code
--------------------------------------------------------------------------------

3913 sub print_price_nicely {
3914 my ($model,$link,$price) = (@_);
3915 return unless ($price);
3916 if ($price) {
3917 if ($price eq "") {

--------------------------------------------------------------------------------

This is giving the error:

"Use of uninitialized value in concatenation (.) or string at
cgi/phones.pl line 3917."


How can the script reach line 3917 and then report "uninitialized
value"?!?!?

Are there else-clauses?

Anno
 
T

Tad McClellan

Chris Conwell said:
I've been coding in Perl for some years now but here's a snippet of
code that's got me really stumped:

--------------------------------------------------------------------------------
Code
--------------------------------------------------------------------------------

3913 sub print_price_nicely {
3914 my ($model,$link,$price) = (@_);
3915 return unless ($price);
3916 if ($price) {
3917 if ($price eq "") {


It is not an "error". It is a "warning". There is a difference.

"Use of uninitialized value in concatenation (.) or string at
cgi/phones.pl line 3917."


How can the script reach line 3917 and then report "uninitialized
value"?!?!?


Some things are warn()able and some things aren't.

Testing the "truth" of a variable whose value is undef
is not warnable (3915 & 3916).

Testing the "string equality" of a variable whose value is undef
against a constant string _is_ warnable (3917).
 
P

Paul Lalli

It is not an "error". It is a "warning". There is a difference.


Some things are warn()able and some things aren't.

Testing the "truth" of a variable whose value is undef
is not warnable (3915 & 3916).

Testing the "string equality" of a variable whose value is undef
against a constant string _is_ warnable (3917).

None of which gives any kind of answer. The point of the OP's question is
that clearly Perl believes on line 3915 that $price has a true value,
and on 3916 that $price has a true value. But the compilation warning
he's getting indicates that $price is unintialized on line 3917.

No obviously, this isn't correct, and the warning isn't reporting the
correct line. But the statements you posted have nothing to do with the
problem at hand.

Paul Lalli
 
U

Uri Guttman

CC> 3913 sub print_price_nicely {

i hope you don't actually use 1 char indents. 2 is bad enough with 3 or
4 being a good minimum.

CC> 3914 my ($model,$link,$price) = (@_);

no need for () around @_. the left side provides the list context and
the () are not doing anything.

CC> 3915 return unless ($price);

no need for () there, any expression is fine. only if statements (as
below) need () for proper syntax.

CC> 3916 if ($price) {

why are you again testing $price when you just returned if you have no
price?

CC> 3917 if ($price eq "") {

CC> This is giving the error:

CC> "Use of uninitialized value in concatenation (.) or string at
CC> cgi/phones.pl line 3917."

CC> How can the script reach line 3917 and then report "uninitialized
CC> value"?!?!?

first off, you should never have a source file with so much code in
it. break it up into smaller files as it will make life easier for you
in many ways.

secondly, i bet your line numbering is at fault. how did you determine
those line numbers? perl may have a different view of what line numbers
are than you (or the program you used) do. did your line numbering thing
count long wrapped lines as 1 or multiple lines? perl counts them as one
line.

uri
 
C

Chris Conwell

Tore Aursand said:
Give us the lines in the scope created on line 3917. One other thing is
that you don't need to check 'if ( $price )' and/or 'if ( $price eq "" )'
as long as you do 'return unless ( $price )'.

The 'return unless ( $price )' was only added as a debugging line
after getting completely confused as to how the error message can
happen (and then getting more confused when the error continued :).
(But please remember that '$price' with a value of 0 also will be skipped,
even though I recall extremely cheap is also a price to pay...)

Yes, if $price is zero then an "elsif" further on deals with it - see
next post.

Thanks for taking an interest in this problem.

Chris.
 
C

Chris Conwell

Are there else-clauses?

Yes, the script continues (where "..." signifies irrelevant code to
output data) :

--------------------------------------------------------------------------------

3913 sub print_price_nicely {
3914 my ($model,$link,$price) = (@_);
3915 return unless ($price);
3916 if ($price) {
3917 if ($price eq "") {
....
3920 }
3921 elsif ($price =~ /TBC/i) {
....
3925 }
3926 elsif ($this_index =~ /INDEXU/) {
....
3929 }
3930 else {
....
3935 }
3936 }

--------------------------------------------------------------------------------

Are you thinking that the error is actually on one of the "elsif"
statements and is being incorrectly flagged as the first "if" line?

Thanks for your help,


Chris.
 
U

Uri Guttman

CC> 3916 if ($price) {
CC> 3917 if ($price eq "") {
CC> ...
CC> 3920 }
CC> 3921 elsif ($price =~ /TBC/i) {
CC> ...
CC> 3925 }
CC> 3926 elsif ($this_index =~ /INDEXU/) {
CC> ...
CC> 3929 }
CC> 3930 else {
CC> ...
CC> 3935 }
CC> 3936 }

CC> --------------------------------------------------------------------------------

CC> Are you thinking that the error is actually on one of the "elsif"
CC> statements and is being incorrectly flagged as the first "if" line?

that is very possible. perl does sometimes report line numbers in odd
places and this could be one of them. so investigate $this_index's value

but i also asked if the way you generated line numbers are accurate and
handled long lines. you never asnwered that.

uri
 
J

John Bokma

Uri said:
CC> 3916 if ($price) {
CC> 3917 if ($price eq "") {
CC> ...
CC> 3920 }
CC> 3921 elsif ($price =~ /TBC/i) {
CC> ...
CC> 3925 }
CC> 3926 elsif ($this_index =~ /INDEXU/) {
CC> ...
CC> 3929 }
CC> 3930 else {
CC> ...
CC> 3935 }
CC> 3936 }

CC> --------------------------------------------------------------------------------

CC> Are you thinking that the error is actually on one of the "elsif"
CC> statements and is being incorrectly flagged as the first "if" line?

that is very possible. perl does sometimes report line numbers in odd
places and this could be one of them. so investigate $this_index's value

I remember this being a bug (got bitten by it a few times about a year
ago), and it is indeed elsif related. Couldn´t find it with Google though.
 
C

Chris Conwell

Uri Guttman said:
i hope you don't actually use 1 char indents. 2 is bad enough with 3 or
4 being a good minimum.

No, that's just how it appeared when I cut and pasted into Google
Groups, by default Homesite uses 5.
no need for () around @_. the left side provides the list context and
the () are not doing anything.

CC> 3915 return unless ($price);

no need for () there, any expression is fine. only if statements (as
below) need () for proper syntax.

Thanks, never realised that before.
CC> 3916 if ($price) {

why are you again testing $price when you just returned if you have no
price?

As per previous posts, some apparently superfluous lines were added to
aid debugging when Perl appeared to me to be giving an impossible
error message.
secondly, i bet your line numbering is at fault. how did you determine
those line numbers? perl may have a different view of what line numbers
are than you (or the program you used) do. did your line numbering thing
count long wrapped lines as 1 or multiple lines? perl counts them as one
line.

I wish it were that simple but unfortunately its not. The line
numbering is definitely correct and Homesite quite happily handles
line/word wrap.

Thanks for the input.


Chris.
 
C

Chris Conwell

John Bokma said:
I remember this being a bug (got bitten by it a few times about a year
ago), and it is indeed elsif related. Couldn´t find it with Google though.

Bingo! Yes, it appears to be a bug in that it was actually one of the
"elsif" lines further on causing the problem.

Many thanks for everyone's help in fixing this annoying problem.


Chris.
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top