Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Here then is an SSCCE which works for me. It shows both the broken output which you should be seeing at the moment and the fixed output after it is properly encoded. I've taken the liberty of cleaning up some of your code and removing all the HTML-generating functions from as those are deprecated. In fact I've removed entirely since you weren't using it for anything else.

#!/usr/bin/env perl use utf8; use strict; use Text::CSV; use Encode 'encode'; ### read the output my $resultsFile = "results.txt"; open my $fh, "<:encoding(utf8)", $resultsFile or die "cannot open re +sults file $resultsFile for reading: $!"; my @urls = grep {/\/search\//} <$fh>; close ($fh); print "Content-type: text/html; charset=utf-8\n\n"; print "<h2>Pre-sort</h1><ul>"; print "<li>$_</li>" for @urls; print "</ul>\n"; print "<h2>Same, but encoded</h2><ul>"; print encode ('UTF-8', "<li>$_</li>") for @urls; print "</ul>\n"; # sort @urls based on the search string my @sorted_urls = map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { m|/search/\s*([^\?]+)\?|; [$_, $1] } @urls; my $csv = Text::CSV->new ({binary => 0, sep_char => "|"}); print "<h2>Broken sorted</h2>\n"; # parse and print print '<table>'; foreach my $row (@sorted_urls) { # print TEMP $row; $csv->parse ($row); my @els = $csv->fields; $els[0] =~ /\/search\/(.+)\?scope=/i; my ($term) = $1; my ($link) = $els[0]; print "<tr>"; print qq#<td><a href="$link" target="_blank">$term</a></td>#; print "<td>$_</td>\n" for @els[1 .. 4]; print "</tr>\n"; } print '</table>'; print "<h2>Same, but encoded</h2>"; # parse and print print '<table>'; foreach my $row (@sorted_urls) { # print TEMP $row; $csv->parse ($row); my @els = $csv->fields; $els[0] =~ /\/search\/(.+)\?scope=/i; my ($term) = $1; my ($link) = $els[0]; print "<tr>"; print encode ('UTF-8', qq#<td><a href="$link" target="_blank">$term</a></td>#); print "<td>$_</td>\n" for @els[1 .. 4]; print "</tr>\n"; } print '</table>';

Hopefully you can see here that you just need to ensure that you properly encode the output. There are many ways to do this, I've picked one here which is very explicit, for your benefit. See Encode for lots more on how to use this module.

In reply to Re^17: Text::CSV encoding parse() by hippo
in thread Text::CSV encoding parse() by slugger415

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, 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, 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.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (9)
    As of 2019-10-21 14:52 GMT
    Find Nodes?
      Voting Booth?