uninitialized value in concatenation ...

Discussion in 'Perl Misc' started by Chris Conwell, Apr 1, 2004.

  1. 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
    Chris Conwell, Apr 1, 2004
    #1
    1. Advertising

  2. Chris Conwell

    Tore Aursand Guest

    On Thu, 01 Apr 2004 06:19:51 -0800, Chris Conwell wrote:
    > 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...)

    :)


    --
    Tore Aursand <>
    "Fighting terrorism is like being a goalkeeper. You can make a hundred
    brilliant saves but the only shot that people remember is the one that
    gets past you." -- Paul Wilkinson
    Tore Aursand, Apr 1, 2004
    #2
    1. Advertising

  3. Chris Conwell

    Anno Siegel Guest

    Chris Conwell <> wrote in comp.lang.perl.misc:
    > 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
    Anno Siegel, Apr 1, 2004
    #3
  4. Chris Conwell <> wrote:
    > 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:



    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).


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Apr 1, 2004
    #4
  5. Chris Conwell

    Paul Lalli Guest

    On Thu, 1 Apr 2004, Tad McClellan wrote:

    > Chris Conwell <> wrote:
    > > 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:

    >
    > 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).


    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
    Paul Lalli, Apr 1, 2004
    #5
  6. Chris Conwell

    Uri Guttman Guest

    >>>>> "CC" == Chris Conwell <> writes:

    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
    Uri Guttman, Apr 1, 2004
    #6
  7. Tore Aursand <> wrote in message news:<>...

    > 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.
    Chris Conwell, Apr 1, 2004
    #7
  8. -berlin.de (Anno Siegel) wrote in message news:<c4hbqn$ce3$-Berlin.DE>...
    > > How can the script reach line 3917 and then report "uninitialized
    > > value"?!?!?

    >
    > 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.
    Chris Conwell, Apr 1, 2004
    #8
  9. Chris Conwell

    Uri Guttman Guest

    >>>>> "CC" == Chris Conwell <> writes:

    >> Are there else-clauses?

    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
    Uri Guttman, Apr 1, 2004
    #9
  10. Paul Lalli <> wrote:
    > On Thu, 1 Apr 2004, Tad McClellan wrote:


    [ snip my drivel ]

    > But the statements you posted have nothing to do with the
    > problem at hand.



    Oops.

    Sorry.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Apr 1, 2004
    #10
  11. Chris Conwell

    John Bokma Guest

    Uri Guttman wrote:

    >>>>>>"CC" == Chris Conwell <> writes:

    >
    >
    > >> Are there else-clauses?

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

    --
    John personal page: http://johnbokma.com/

    Freelance Perl / Java developer available - http://castleamber.com/
    John Bokma, Apr 2, 2004
    #11
  12. Uri Guttman <> wrote in message news:<>...

    > 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.
    Chris Conwell, Apr 3, 2004
    #12
  13. John Bokma <> wrote in message news:<406d007b$0$24338$>...

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


    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.
    Chris Conwell, Apr 3, 2004
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. G Kannan
    Replies:
    1
    Views:
    1,234
    Eric J. Roode
    Oct 11, 2003
  2. Sukhbir Dhillon
    Replies:
    1
    Views:
    6,252
    Joe Smith
    Apr 5, 2004
  3. smartins68
    Replies:
    1
    Views:
    6,003
    Joe Smith
    Jun 9, 2004
  4. S
    Replies:
    3
    Views:
    327
    Gunnar Hjalmarsson
    Feb 3, 2004
  5. Jesse Cary
    Replies:
    2
    Views:
    220
    Jürgen Exner
    Oct 15, 2004
Loading...

Share This Page