So, can any monk would suggest me any better way to write this code that is more robust, with use strict'.
This produces the same output as the original and works with warnings and strict enabled:

Update: fixed an edge case in sub read_file.

#!/usr/bin/perl use warnings; use strict; use GD; my ( $min1, $max1, $x1, $y1, $lines ) = read_file( 'fwd.stb' ); # Define constants my $X1start = 50 + 20; # X-axis my $Y1start = 300; # X-axis my $X1end = 450 + $lines - 400; # X-axis my $Y1end = 300; # X-axis my $X2start = 50 + 20; my $Y2start = 100; my $X2end = 50 + 20; my $Y2end = 300; # create a new image my $im = new GD::Image( $X1end + 50, $Y2end + 100 ); # allocate some colors my $white = $im->colorAllocate( 255, 255, 255 ); my $green = $im->colorAllocate( 0, 255, 0 ); my $black = $im->colorAllocate( 0, 0, 0 ); my $red = $im->colorAllocate( 255, 0, 0 ); my $blue = $im->colorAllocate( 0, 0, 255 ); my $half_lines = int( $lines / 2 ); # set the caption for the graph $im->string( gdLargeFont, $half_lines - 50, 20, 'ENERGY GRAPH', $black + ); # draw the y-axis & x-axis $im->line( $X2start, $Y2start, $X2end, $Y2end, $black ); $im->line( $X1start, $Y1start, $X1end, $Y1end, $black ); # set the caption for x-axis and y-axis $im->string( gdLargeFont, $half_lines - 50, 335, 'NUCLEOTIDE POSITIO +N', $black ); $im->stringUp( gdLargeFont, 5, 260, 'FREE ENERGY(delta G)', $black ); $im->string( gdSmallFont, $half_lines + 100, 20, ' Genomic Sequence - +------', $red ); $im->string( gdSmallFont, $half_lines + 100, 40, ' Shuffled Sequence - +------', $blue ); my ( $min2, $max2, $x2, $y2 ) = read_file( 'total_shuffle.stb' ); my $min = $min1 <= $min2 ? $min1 : $min2; my $max = $max1 >= $max2 ? $max1 : $max2; graph( $x1, $y1, $min, $max ); open my $DISPLAY, '|-:raw', 'display', '-' or die "Cannot open pipe to + 'display' $!"; print $DISPLAY $im->png; close $DISPLAY or warn $! ? "Error closing 'display' pipe: $!" : "Exit + status $? from 'display'"; exit 0; sub read_file { my $file_name = shift; open my $fh, '<', $file_name or die "Cannot open '$file_name' $!"; my ( $min, $max, @x, @y ); while ( <$fh> ) { my @value = split; push @x, $value[ 0 ] - 7; push @y, $value[ 1 ]; if ( @y > 1 ) { $max = $y[ -1 ] if $max < $y[ -1 ]; $min = $y[ -1 ] if $min > $y[ -1 ]; } elsif ( @y == 1 ) { $min = $max = $y[ -1 ]; } } return $min, $max, \@x, \@y, $.; } sub graph { my ( $x_ref, $y_ref, $minEnergy, $maxEnergy ) = @_; # Scaling the values my $Yscale = ( $Y2end - $Y2start ) / ( abs( $minEnergy ) - abs( $m +axEnergy ) ); my $scale = ( $minEnergy - $maxEnergy ) / 5; my $incEnergy = $maxEnergy; while ( $incEnergy > $minEnergy ) { ##### Prob loop#### my $x1 = $X1start - 2; my $y1 = int( ( ( abs( $incEnergy ) - abs( $maxEnergy ) ) * $Y +scale ) + $Y2start ); my $x2 = $X1start + 2; $im->line( $x1, $y1, $x2, $y1, $black ); my $displayEnergy = sprintf '%2.1f', $incEnergy; $im->string( gdSmallFont, $x1 - 35, $y1 - 10, $displayEnergy, +$black ); $incEnergy = $incEnergy + $scale; } # mark 10 points on the position axis my $posSkip = int( @$x_ref / 10 ); # If the length of the fragment is lesser than the x-axis my $skipPixel = ( $X1end - $X1start ) / @$x_ref; my $count; for my $i ( 0 .. $#$x_ref - 1 ) { my $x1 = int( $i * $skipPixel + $X1start ); my $y1 = int( ( abs( $y_ref->[ $i ] ) - abs( $maxEnergy ) ) * +$Yscale + $Y2start ); my $x2 = int( ( $i + 1 ) * $skipPixel + $X1start ); my $y2 = int( ( abs( $y_ref->[ $i + 1 ] ) - abs( $maxEnergy ) +) * $Yscale + $Y2start ); $im->line( $x1, $y1, $x2, $y2, $red ); $count++; if ( $count == $posSkip ) { $x1 = int( $i * $skipPixel + $X1start ); $y1 = $Y1end - 2; $x2 = int( $i * $skipPixel + $X1start ); $y2 = $Y1end + 2; $im->line( $x1, $y1, $x2, $y2, $black ); $im->string( gdSmallFont, $x1, $y1 + 10, $i + 1, $black ); $count = 0; } } } __END__

In reply to Re: Prob with 'while' loop and subroutine calling by jwkrahn
in thread Prob with 'while' loop and subroutine calling by cool

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.