Although you use strict, you don't use warnings. If you had you'd have received a slew of 'Use of uninitialized value' warnings.

However, there are a bundle of problems and areas for improvement from using lexical file handles and three parameter open to processing input data and checking parameters. The following addresses those issues:

use strict; use warnings; my $data = <<DATA; 4,8,3 2,2,3 -1,2,8 DATA open my $inFile, '<', \$data or die "File open failed: $!\n"; my @temp = map {[split ',']} <$inFile>; close $inFile; for my $values (@temp) { my $quadratic = quadratic (@$values); if (! defined $quadratic) { printf "a = %f, b = %f, c = %f: No solution\n", @$values; next; } printf "a = %f, b = %f, c = %f: %f\n", @$values, $quadratic; } sub quadratic { my ($a, $b, $c) = @_; my $root = $b*$b - 4*$a * $c; return if ! $a; return if $root < 0; return -($b + (0 < $b ? 1 : -1) * sqrt($root)) / 2*$a; }

Prints:

a = 4.000000, b = 8.000000, c = 3.000000: -24.000000 a = 2.000000, b = 2.000000, c = 3.000000: No solution a = -1.000000, b = 2.000000, c = 8.000000: 4.000000

True laziness is hard work

In reply to Re: solving a quadratic equation by GrandFather
in thread solving a quadratic equation by vis1982

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.