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

Monks:

I thought I posted this last night before our power went out...long story...

I'm working on a web application. In one script, I have a subroutine that creates three variables. One is an array ref created via an external module function call. The second is a long string of alpha-numeric strings extracted from that array ref, pushed to a temporary array and single-quoted and joined.

The third one is the problem: it's a very simple count of elements in the aforementioned temporary array. Like this:

$count = @array;

We have tested for the value of $count, and it's a number, just as one would expect.

We are trying to return the three variables to the caller in a later part of the script. We call thusly:

($aref, $count, $string) = MySub;

and we return from MySub:

return ($aref, $count, $string);

Basic. Problem is when we include $count, in any position, even alone, the script hangs. The debugging setup the client uses is primitive and basically useless, since it only returns debug and trail information to the web page...and if the web page hangs, we can't see what's happening.

When we remove that one variable, everything works fine.

We probably don't need to return this value, and we can find a workaround if necessary. But I had to wonder if anyone had experienced anything like this and had a clue as to why this might happen. It's a number.

This same application has plenty of functions that return all kinds of variables all over the place (since I wrote many of them), and everything else works peachy.

I appreciate any wisdom on this head-scratcher.

Replies are listed 'Best First'.
Re: Return variable hangs script
by monarch (Priest) on Jul 03, 2007 at 11:42 UTC
    Another simple question.. did you include use strict; in your code? If not, then a number of side effects could be getting you.

    Either way, check to make sure you haven't defined a function called "count" or imported a package which exports count as a function or variable.

      strict wasn't used in this particular script, so I'll add that and see what happens. The other items you mentioned were carefully checked. I should mention that I'm working on a project converting older Perl code (which was converted from ColdFusion...), created by people who really don't know Perl all that well. I can barely get them to understand what a reference is, so getting them to use strict with any consistency is like pulling teeth.
Re: Return variable hangs script
by clinton (Priest) on Jul 03, 2007 at 11:22 UTC
    Complete guess, but are you sure that @array is a normal Perl array? It's not tied, or created with XS?

    Try dumping the variables with Data::Dump::Streamer before returning.

    Clint

      Yes, it's a normal array. We create it as a "placeholder" to build the longer string. The array ref contains array refs of strings from a database column, that look like this:

      12345678-abcd-4321-wxyz-wufht83nwi03

      They're all unique. We need to fetch all the strings, then quote them and string them together. That resulting long string is passed to a subsequent SQL query as an IN ('...') conditional.

      Here's what I'm doing, in more detail:

      my ($aref, $acount, @temparray, $tcount);

      $aref = GetArrayRef($dbh,$sqlquery);

      $acount = @$aref; # results in n

      foreach (@$aref) { push(@temparray,qq('$_[0]') ); }

      $tcount = @temparray; # should also equal n, and does.

      After we create @temparray, we join all the elements to a string. We have to quote the strings or else Oracle will not parse the query correctly.

      The problem is that when we return $tcount to the caller, it hangs. $acount passes fine.

      I've double checked everything, including any called modules, and everything seems fine.

      Odd.
Re: Return variable hangs script
by leocharre (Priest) on Jul 03, 2007 at 12:28 UTC
    I don't know what your debug is .. but here's what I always do for stuff like this..
    use strict; use warnings; my ($x, $y, $z) = returner(); sub returner { my ($a, $b, $c); # define $a $b and $c # now for something I would throw in just for kicks # just before you return.. defined $a or die("a not defined"); defined $b or die("b not defined"); defined $c or die("c not defined"); return ( $a, $b, $c); }
    Something else that might possibly help
    use strict; use warnings; my ($x, $y, $z) = @{returner()}; sub returner { my ($a, $b, $c) =( undef, undef, undef); # define $a $b and $c # now we return one value only return [$a, $b, $c]; }
      Thanks for th advice. I'll try that.

      As I said in the original question, this isn't a show-stopper for us. But my curiosity is piqued over why it wouldn't work.

      I'll try and pass along the results.

      Thanks to all for your input!
Re: Return variable hangs script
by tirwhan (Abbot) on Jul 03, 2007 at 12:10 UTC
    The third one is the problem: it's a very simple count of elements in the aforementioned temporary array. Like this: $count = @array;

    That doesn't assign a reference to an array, it puts the length of the array @array into $count What you want (and are probably doing in your real code) is

    $count = \@array;

    Other than that, I'd start by inspecting the value that actually ends up in $count to see what it contains. You can use the inbuilt ref function or Scalar::Util->reftype(). Also consider posting the real code you're using (since you've obviously made at least one mistake in transcribing/summarising it), it sould be particularly pertinent to know which "external module" returns the array reference.


    All dogma is stupid.
      You misunderstood. I'm not trying to assign the array to a ref. I already have a ref. The array is just a place holder to take elements from the original array ref so we can string them together. (See my more extended code in the first response above).

      What I'm doing with $count = @array is just get a count of the elements in @array after we stick them in there. That's what I want.

      And I've examined $count and it equals n, the number of elements, which is exactly what I want. A number.
Re: Return variable hangs script
by Moron (Curate) on Jul 03, 2007 at 11:29 UTC
    I'd post the subroutine and as much of the code calling it as possible - the information posted so far doesn't cause it IMO.
    __________________________________________________________________________________

    ^M Free your mind!

      see my response above...
Re: Return variable hangs script
by ForgotPasswordAgain (Vicar) on Jul 04, 2007 at 09:46 UTC

    Do you have access to the error_log?

    I'm guessing that if you don't use strict, maybe you also don't use my so you have global variables all over the place. Is $count a lexical variable, or are you overwriting a global one? For example, if $count determines when a while loop stops, but you kept resetting it to a non-zero value.....