Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Use perl CGI to create <a href="url" target="t">text</a>?

by misterperl (Pilgrim)
on Jun 08, 2022 at 13:05 UTC ( [id://11144515]=perlquestion: print w/replies, xml ) Need Help??

misterperl has asked for the wisdom of the Perl Monks concerning the following question:

Hello Beloved Monks:

I researched this , and the only answer I find is to abandon CGI.pm for this particular tag, and to just "print" the anchor. Even on perldoc.org:

...Something like this will do the trick. $myself = self_url; print "<a href=\"$myself#table1\">See table 1</a>"; <=== really?

I did find one site that implied there was a $cgi->a() method, but I found no documentation for it, nor how to close the tag with /a. One slashdot suggestion was to ^F for anchor on the CGI docs, and it came back RED. Is the practice truly to abandon CGI.pm for this requirement and instead print? I've become disciplined to use Perl CGI to generate all HTML , so this seems very unnatural.

Regards to all Monks!

Replies are listed 'Best First'.
Re: Use perl CGI to create <a href="url" target="t">text</a>?
by choroba (Cardinal) on Jun 08, 2022 at 13:26 UTC
    HTML generating functions have been moved to CGI::HTML::Functions. $cgi->a isn't documented there, but the example right at the beginning of the linked section should be enough to understand how it works.

    Nowadays, we usually use templates to generate HTML (Template::Toolkit, Text::Xslate). Going back to print is not the right way.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      TYVM yes I see that and got it working ! Well done..

      Nowadays, we usually use templates to generate HTML (Template::Toolkit, Text::Xslate). Going back to print is not the right way.

      a template by any other name ;)

      since it started in ~1995 it didn't take a lot of work with "CGI::HTML::Functions" to find bugs, then check the bug cue/Changes, and realize bugs remain and reappear way too frequently to be relied upon, so you start writing your own functions and soon

      soon you turn it into heredocs with escapeHTML... There is a reason a diy templating engine is a rite of passage

Re: Use perl CGI to create <a href="url" target="t">text</a>?
by Fletch (Bishop) on Jun 08, 2022 at 13:29 UTC

    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.

      Thanks, my question wasn't how or if to print. It was about how or if to use $cgi->a() which was answered but TY.
Re: Use perl CGI to create <a href="url" target="t">text</a>?
by BillKSmith (Monsignor) on Jun 08, 2022 at 15:22 UTC
    If, contrary to the advice you have been given you insist on using CGI.pm, then I recommend that you buy the a copy of the ancient companion book "A Complete Guide to Programming with CGI.pm" (ISBN 0471247448). Your current question is answered in "Chapter Two: CGI.pm Basics"
    Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11144515]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-20 00:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found