Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (9)
As of 2024-04-24 10:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found