Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have no idea what is happeneing.. but I have a 200 line long subroutine for a banner script I am working on

basicly, it checks to see if the banner its getting ready to use is already on the page, and if it is, goes to the next one, and i also have it working in categories, so i am calling the routine with

&banner(category).. and it will return the variables for the first availiable banner for that category. I also have it writing the banner it used to a file.

I may call this routine more than once in my scripts.. and the odd thing about this is.. after I run it more than twice, it always looses $var for some stupid reason.

sub banner { my $var = shift; }
and I have no idea why... I do know that I am not altering $var AT ALL during the subroutine, except for the initial $var = shift

I am at a loss, I am willing to put the code for my subroutine up, if there is a suitable place to put 200 lines of code up.

if you have any ideas at all, please help, thanks

Replies are listed 'Best First'.
Re: Crazyness
by thraxil (Prior) on Jul 18, 2001 at 21:08 UTC

    this is the sort of thing that a debugger is designed to help with. if you haven't used the perl debugger before, it's never too soon to aquaint yourself with '-d'. (see 'perldoc perldebug'). set a breakpoint at the beginning of the function and step through, checking the value of $var as you go to find out where you lost it. it's almost guaranteed to be a faster way than waiting for us to pick apart your code for you. even if it doesn't help you, it will probably point you towards a much more specific question to ask.

    anders pearson

      Please note that good programmers disagree with each other on how valuable debuggers are. In the thread starting at Are debuggers good? you will find discussion of both points of view.

      In this case the most obvious problem is the fact that the subroutine is 200 lines long. While I don't like to say that there are absolute rules in programming, whenever the section of code that you are working with does not all fit on your screen at once, bug counts skyrocket. Furthermore a 200 line subroutine has a lot of behaviour encapsulated in a form where you cannot easily work through and fix each function, again a bad thing.

      The usual rule of thumb is that 50 lines should be the maximum function length. The average that I quote for myself is about 10 lines.

      I would far prefer to point a person in that direction than tell them to debug in a way that I personally don't.

Re: Crazyness
by Russ (Deacon) on Jul 18, 2001 at 21:00 UTC
    Maybe you could start by posting all the lines where you call the subroutine (banner($category)). The entire subroutine may not be necessary.

    Also, are you warning $var so you can see what happens to the value when it "disappears?" Is it an empty string or undefined? Does it disappear somewhere in the sub, or does it never make it in?

    Russ
    Brainbench 'Most Valuable Professional' for Perl

Re: Crazyness
by suaveant (Parson) on Jul 18, 2001 at 21:03 UTC
    By losing $var, you mean that the 3rd, 4th etc call to banner results in $var being empty? If so... are you sure you are passing data in? You may want to put a
    sub banner { die "Not enough arguments to banner" unless @_; my $var = shift; }
    otherwise, when is it lost? If $var never holds a value (i.e. directly after the $var = shift;) then I would look in the calling code, not the subroutine for the problem. If it gets set and goes away, print it at various spots and find out where it goes away...

                    - Ant

Re: Crazyness
by cLive ;-) (Prior) on Jul 18, 2001 at 21:02 UTC
    So, by deduction, you must be changing the (category) part of the &banner call.

    You do send that each time you call the sub, don't you?

    cLive ;-)