Unquoted heredoc terminators in the << are not nice. And you can add another step:
my $y = 0; my @list; foreach (@scores) { if ($_ != 0) { push(@list, $y++, @list, $_ * -1.0); } } my $list = join(",",@list); print << "SVG"; <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"> <svg height="200" width="200"> <line x1="0" y1="0" x2="80" y2="0"/> <polyline points="$list" style="stroke: black; width: 1px; fill: non +e;"/> </svg> SVG
And now we're moving in the right direction - separate the application logic from the output logic. Let's go another step towards it:
my $y = 0; my @list; foreach (@scores) { if ($_ != 0) { push(@list, $y++, @list, $_ * -1.0); } } print << "SVG"; <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"> <svg height="200" width="200"> <line x1="0" y1="0" x2="80" y2="0"/> <polyline points="@{[ join(",", @list) ]}" style="stroke: black; width: 1px; fill: none;"/> </svg> SVG
Finally we wrap up by collapsing the application logic:
my @list = map { $scores[$_] != 0 ? ($_, $scores[$_] * -1.0) : () } 0 .. $#scores;

No extra temporary variables, no scattered output, application logic minimized. The next step, assuming this is part of a larger app, would be Template Toolkit.

Actually, thinking about it, it's debatable whether the != 0 distinction is part of the application logic or part of the display logic. One might just as well do

my @l_list = map [ $_, $scores[$_] * -1.0 ], 0 .. $#scores; print << "SVG"; ... @{[ join ",", map @$_, grep $_->[0] != 0, @l_list; ]} ...

Which leads me to question my term "application logic" - one might just as well consider the pairing of data points and coordinates display logic and shift it all into the template. That depends on the specific situation at hand.

At any rate, this is a very important distinction to make, and to make consciously. I see failure to do so all the time even though it's just as important everywhere else as in web related programming. Maintainability and clarity is best achieved if you do exactly one cleanly defined thing at a time in every piece of your code. Be careful not to mix up activities. Of course the entire concept hinges on the idea of clean data structure design and in a way, that means clean interfaces between parts of your code. All of these matters are interrelated, and mastering them is what makes one a great software engineer.

Makeshifts last the longest.


In reply to Re^2: Refactoring Redux... by Aristotle
in thread Refactoring Redux... by hsmyers

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.