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

Update: Thanks to fglock for answering my question and getting me back to work!

I'm trying to use the larrd graphing tool to output a graph of data in an RRD. The larrd script contains this hash:
my(%graph_c) = ( SQLDB_Server => { hourly => { start_sec => "e-48h", title => "$host $titleDesc Last 48 Hours", }, yaxis => $yaxisDesc, lines => sub { my ($RRD,$service,$graph,$p,$color)=@_; $RRD =~ s/:/\\:/g; return [ qq{-u 5},qq{-l 0}, qq{DEF:stat1=$RRD:$table:AVERAGE}, ** qq{LINE2:stat1#0000FF:$lineName} ] }, }, );
It calls this hash on this line:
@defs = (@defs,@{&{$graph_c{$service}{'lines'}}($RRD,$service,$graph,$p++,&color() )});
With the above setup, the graph comes out fine. I would like to be able to dynamically add multiples of the line marked **. If I try to store the various qq'd strings into a variable, adding as many ** lines as I'd like, then return the variable, the script crashes in apache giving premature header errors.
I am unable to understand exactly what is being returned in the sub, and how to mimic it while returning a variable instead.

Replies are listed 'Best First'.
Re: Difference in returning string "x" vs string "x" in a variable
by fglock (Vicar) on Nov 04, 2004 at 19:09 UTC

    Does this make it crash?

    my @r = ( qq{-u 5},qq{-l 0}, qq{DEF:stat1=$RRD:$table:AVERAGE}, qq{LINE2:stat1#0000FF:$lineName}, ); return \@r;

    If it works, then try adding this extra line:

            push @r, qq{LINE2:stat1#0000FF:$lineName};
      fglock, your solution worked perfectly.
      I knew there must be a way to mimick that return statement, just couldn't figure it out.
      Thank you very much, you've saved me alot of time and effort.
Re: Difference in returning string "x" vs string "x" in a variable
by bpphillips (Friar) on Nov 04, 2004 at 18:57 UTC
    Have you looked at your web servers error logs to get the more specific error message? Have you tried adding:
    use CGI::Carp qw(fatalsToBrowser);
    to the beginning of your script? Have you tried running your script from the command line to see the error it gives you there? The generic "premature headers" usually indicates a very basic problem with the script (it's dieing because of a syntax error or something similar). Posting the code that doesn't work would help us out in determining what the problem is.

    --Brian
Re: Difference in returning string "x" vs string "x" in a variable
by Joost (Canon) on Nov 04, 2004 at 18:59 UTC
    Adding more of those lines should work*) as long as you remember to put a comma at the end of the line.

    Also, usually error messages for CGI scripts are written to the servers error log. You could have looked there or use CGI::Carp qw(fatalsToBrowser); or run the script from the command line.

    *) where work means: it will compile and run and return that extra line from the sub.

Re: Difference in returning string "x" vs string "x" in a variable
by elwarren (Priest) on Nov 04, 2004 at 19:10 UTC
    How are you creating your array? Are you pushing qq strings on? How are you returning it?
Re: Difference in returning string "x" vs string "x" in a variable
by Matt G (Initiate) on Nov 04, 2004 at 19:12 UTC
    Thanks for the heads up, sorry I didn't have enough information in my original question. I will go and work through the very suggestions. Thank you.