How do you think CGI generates HTML output if not by printing it? And the documentation itself uses print in several examples (explicitly printing anchors, in fact).

It should take just a second to work out from the _tag_func sub in the code to work out that attributes for tag generation should be passed as a hashref followed by the contents.

$ perl -MCGI -E 'say CGI->new->a( {href => "http://example.com"}, "Sam +ple" )' <a href="http://example.com">Sample</a>

That being said in general it'd be better practice to use a templating engine for your HTML generation with that taking any dynamic values from whatever context its passed. If you look at Mojolicious and the like (Dancer2 (?), edit Dancer2::Template::TemplateToolkit specifically maybe) those use that kind of approach typically.

The cake is a lie.
The cake is a lie.
The cake is a lie.


In reply to Re: Use perl CGI to create <a href="url" target="t">text</a>? by Fletch
in thread Use perl CGI to create <a href="url" target="t">text</a>? by misterperl

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.