Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: "Useless use of private variable in void context"

by BrowserUk (Patriarch)
on Jun 20, 2003 at 21:35 UTC ( [id://267720]=note: print w/replies, xml ) Need Help??


in reply to "Useless use of private variable in void context"

I concur with chromatic that the line you posted almost certainly isn't the line that is causing the error.

The binary chop technique of commenting out lumps of code is a very useful one, but isn't always practical. Often commenting out one block of code early in the routine, causes so many errors later in the code that the compiler aborts before it ever gets to the error you're trying to find. Another technique I picked up somewhere is to introduce a deliberate but benign error, one that won't cause the compiler to abort, and then move this slowly through the code until it transitions from appearing before the error you're trying to track down to after.

Here it is before

#! perl -slw use strict; my $deliberate_error=''; my $i; length $deliberate_error; $i; __END__ D:\Perl\test>test Useless use of length in void context at D:\Perl\test\test.pl8 line 7. Useless use of private variable in void context at D:\Perl\test\test.p +l8 line 9.

And here after

#! perl -slw use strict; my $deliberate_error=''; my $i; $i; length $deliberate_error; __END__ D:\Perl\test>test Useless use of private variable in void context at D:\Perl\test\test.p +l8 line 7. Useless use of length in void context at D:\Perl\test\test.pl8 line 9.

Simplistic, but it can sometimes be very effective.

Wouldn't it be nice if the error reported was

Useless use of $i in void context at ....


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


Replies are listed 'Best First'.
Re: Re: "Useless use of private variable in void context"
by Seumas (Curate) on Jun 20, 2003 at 21:42 UTC

    Yes, thank you! I'll attempt this. I hate to show my ignorance, but I'm not clear on the "binary technique" chromatic mentioned? Is there more to it than just chopping out lines and running perl -wc on it? And yes, if I comment out a chunk of this sub, it's going to give a *lot* of compile errors. *grin*

    Off I go to try this out.

      Basically, the binary chop means

      1. Comment out the top half of the code

        Compile -- has the error gone away?

        1. Yes -- It is in the top half of the program.

          So now uncomment the second quarter. Did it come back?

          1. Yes. It is in that second quarter.

            Uncomment the first half and the 3rd eight of the code. Is it still there?

            1. Yes. ....
            2. No. ....
          2. No. It must be in the first quarter

            So uncomment the second eigth....

      2. No. So the error must be in the bottom half of the program.

        So uncomment the first half and comment out the 3rd quarter.

        Compile -- did the error go away?

        1. Yes, It's in the 3rd quarter.

          So uncomment the 6th eigth of the code.

          Compile -- did it come back?

          1. Yes ... Its in the 6th eigth.
          2. No ... Its in the 5th eight.
            1. No ... its in the 4th quarter....

          Damn! That was tedious to type. And probably equally tedious to read for anyone who already knows it, but... I typed it now so there:)


          Examine what is said, not who speaks.
          "Efficiency is intelligent laziness." -David Dunham
          "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


        Aaah. So it's (essentially) just the old print "BREAK"; technique. That's what I thought you meant, but I considered perhaps by 'binary' you meant that I could literally do some sort of 'binary search' for the line in question. Hah! I'm such a dope some times. Thanks for clearing that up. :)
Re: Re: "Useless use of private variable in void context"
by Seumas (Curate) on Jun 20, 2003 at 22:03 UTC

    To clarify, using your method, do I want to have the length error appear physically before/after the error in question - or numerically?

    Also, how is this possible?

    Useless use of private variable in void context at beta.cgi line 2766. Useless use of length in void context at beta.cgi line 2766.

    The 'length' line of code is one line before the GeneratePassword() line, yet they show up as the same line in the error? Is this because the perl interpreter has simplified them into one line of code?

    However, if I place the 'length' line on the very next line after the GeneratePassword() line, I get the following (this is what I'm looking for, correct?

    Useless use of private variable in void context at beta.cgi line 2766. Useless use of length in void context at beta.cgi line 2767.

    So using this method, does it not point to the line my $generated_password = GeneratePassword(); as the culprit?

      As a pure guess, check the line before the GeneratePassword() line and see if you haven't type a comma (,) on the end rather than a semi-colon (;)?

      this is what I'm looking for, correct?

      Essentially yes. I would now move slowly backwards through the code until the line number becomes 2765.

      At that point, you wil have found the limit of the code that perl is taking as being "one line". You can then start looking for things like commas instead of semicolons, unclosed ""s or ''s etc. try commenting out the whole of th e previous line. Does the line number change then?


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


        Okay, I should have been able to start deducing the comma thing by that point, too. I'm not sure why I was so oblivious to it. I did find the culprit however. I had nailed the problem down to 20 lines of code and your comment about a comma (makes absolute sense) lead me to the precise line that was glitching.

        I'm embarrassed to admit this, but the following is the problem that was tripping me up. Note that the code has behaved exactly as wanted and expected for over a year with this line in it - go figure!

        Problematic line:

        if (defined($duplicate_email_address), defined($matched_uid))

        Obvious to anyone with a brain and a set of eyes (except myself apparently), the problem with the above line is that there is a comma where the && operand should be.

        All I can offer in my defense is that I never suspected a problem like that because I'd have expected it to cause a syntax error or prevent compiling.

        I can also now recall the exact step that lead me to this problem over a year ago when I wrote this new batch of code. I had copied the contents of something like my ($foo, $bar, $blah) = $sth->fetchrow_array; and then used that in a conditional and never went back to modify the syntax.

        This was the last thing I was investigating. Every time I went back to check this code and see if I could find the cause of that error, I was looking for things more like a variable that was being defined but never used or something. Gah! So frustrating!

        On a positive side, I rarely make the same mistake twice and I learned a thing or two from this thread. Thanks again, monks.

        it must be somewhere else in the code. I got the same error in the following line chop($fai_in) if(length($fai_in) > 0); when I comment this line got the error in next line. The problem was however was in the following line which missed the brackets while($cust_id, $field_association_id = $sth->fetchrow_array()) which should be while(($cust_id, $field_association_id) = $sth->fetchrow_array()) it solved the problem

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://267720]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-04-18 21:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found