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

my script is executing in shell environment but is not in my browser. Iam using Perl 5.10.0
use CGI ':standard'; use GD::Graph::bars; # Both the arrays should same number of entries. my @data = (["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"], [23, 5, 2, 20, 11, 33, 7, 31, 77, 18, 65, 52]); my $mygraph = GD::Graph::bars->new(500, 300); $mygraph->set( x_label => 'Time taken (Hrs) for compilation', y_label => 'Environments', title => 'Production taken in the Week of $file', ) or warn $mygraph->error; my $myimage = $mygraph->plot(\@data) or die $mygraph->error; open FH, "> graph.png"; binmode FH; print FH $mygraph->plot(\@data)->png; close FH; print "Content-type: image/png\n\n"; print "raja\n"; print $myimage->png;

Replies are listed 'Best First'.
Re: draw a graph
by psini (Deacon) on Jul 31, 2009 at 13:51 UTC

    If the page returns with a server error (5xx) you should check the web server log to see what happened.

    In this case, my best guess is that the server has not permissions to open graph.png for output; in general it is a bad idea using a relative path in a cgi script, for you really don't know which the current dir is at execution time. Try again, specifying an absolute patch name in your open, and a path in which the user running the web server has permission to write into.

    Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."

      ... my best guess is that the server has not permissions to open graph.png for output ...
      It's also a good idea to let yourself know about such problems as they develop. Something along these lines might be informative:
      my $filename = 'graph.png'; open my $fh, '>', $filename or die "opening $filename: $!"; binmode $fh; print $fh $mygraph->plot(\@data)->png or die "writing $filename: $!"; close $fh or die "closing $filename: $!";
        Indeed ... even better would be to use CGI::Carp; to allow the errors &/or warnings to be seen by/in the browser, otherwise they'll only ever be seen in the server logs - but this may be a/the requirement.

        A user level that continues to overstate my experience :-))
Re: draw a graph
by jrsimmon (Hermit) on Jul 31, 2009 at 16:24 UTC

    Where is your shebang? Where is your CGI object? Where are your strictures?

    Updated with working example:
    #!C:\perl\bin\perl.exe -wT #always use strict; use CGI ':standard'; #tell CGI to print fatal errors to the browser use CGI::Carp qw(fatalsToBrowser); use GD::Graph::bars; #you need to actually create a cgi object, not just include the module my $q = new CGI; # Both the arrays should same number of entries. my @data = (["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"], [23, 5, 2, 20, 11, 33, 7, 31, 77, 18, 65, 52]); my $mygraph = GD::Graph::bars->new(500, 300); $mygraph->set( x_label => 'Time taken (Hrs) for compilation', y_label => 'Environments', title => 'Production taken in the Week of $file', ) or warn $mygraph->error; open FH, "> graph.png"; binmode FH; print FH $mygraph->plot(\@data)->png; close FH; #this is all wrong #You're mixing your methods of printing the image. If you're going to #print it directly, there's no need to save it to file. But then you #can't be printing text to the screen first. If you're going to print + it #inside an image tag and referencing a file, you can't just dump the p +ng data #to the screen. #-- you need to look at the cgi doc again #print "Content-type: image/png\n\n"; #print "raja\n"; #print $myimage->png; PRINT_FROM_FILE(); #----OR---- #PRINT_IMAGE_DIRECTLY($mygraph); sub PRINT_FROM_FILE{ #print a simple page referencing the file you created earlier print header , start_html , img ({src=>"/graph.png"}) , end_html; } sub PRINT_IMAGE_DIRECTLY { my $mygraph = pop(@_); # my $myimage = $mygraph->plot(\@data) or die $mygraph->error; print header('image/png') , $mygraph->plot(\@data)->png; }
Re: draw a graph
by Gangabass (Vicar) on Jul 31, 2009 at 14:17 UTC

    I'm wonder why you use this line:

    print "raja\n";