mandar.bobade has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, First of all, this is my question on PerlMonk and really hoping for long lasting association with Perl monks. Here is a question: I have written perl script to handle file and get output in required fashion. e.g. my output is file with columns as follows:
pmid--------Title----------Author 2843847-----XYZ------------ABC 2342545-----LMN------------DEF
Now, I want to create hyperlink to title column, wherein, if I click, say, XYZ then respective pmid link should get open, say like this - http://www.ncbi.nlm.nih.gov/pubmed/2843847. So, question more precisely is, how assign hyperlink to output text using Perl?

Replies are listed 'Best First'.
Re: Hyperlinking output using Perl
by zentara (Cardinal) on Mar 24, 2014 at 10:58 UTC
    See Tk::HyperText for a module to output html, or the code below where it is shown how to manipulate the tags of the Text widget. I would go with the Text widget, as it gives more control.
    #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new( -title => "hyperlinks" ); $mw->fontCreate('big', # -family=>'arial', -weight=>'bold', -size=> 24 ); my $t = $mw->Scrolled('Text', -font => 'big', -bg => 'white', -fg => 'black', )->pack; my $tag = "tag000"; foreach (<DATA>) { chomp; my (@items) = split (/(http:\S+)/,$_); foreach (@items) { if (/(http:\S+)/) { $t->insert( 'end', $_, $tag ); $t->tagConfigure( $tag, -foreground => 'blue' ); $t->tagBind( $tag, '<Any-Enter>' => [ \&manipulate_link, $tag, 'raised', +'hand2' ] ); $t->tagBind( $tag, '<Any-Leave>' => [ \&manipulate_link, $tag, 'flat', 'x +term' ] ); $t->tagBind( $tag, '<Button-1>' => [ \&manipulate_link, $tag, 'sunken' ] +); $t->tagBind( $tag, '<ButtonRelease-1>' => [ \&manipulate_link, $tag, 'raised', undef, \&printm +e ] ); $tag++; } else { $t->insert( 'end', $_ ); } } $t->insert( 'end', "\n" ); } MainLoop; sub printme { local ($,) = " "; print "printme:", @_, "\n"; } sub manipulate_link { # manipulate the link as you press the mouse key my ($a) = shift; my ($tag) = shift; my ($relief) = shift; my ($cursor) = shift; my ($after) = shift; # by configuring the relief (to simulate a button press) $a->tagConfigure( $tag, -relief => $relief, -borderwidth => 1 ); # by changing the cursor between hand and xterm $a->configure( -cursor => $cursor ) if ($cursor); # and by scheduling the specified action to run "soon" if ($after) { my ($s) = $a->get( $a->tagRanges($tag) ); $mw->after( 200, [ $after, $a, $s, $tag, @_ ] ) if ($after); } } __DATA__ Hi there. This is text. THis is more text but http://this.is.a/hyperlink in a line. http://this.is.another/hyperlink followed by http://this.is.a.third/hyperlink __END__

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Hyperlinking output using Perl
by GrandFather (Saint) on Mar 24, 2014 at 06:08 UTC

    What have you tried and how did it fail?

    If the code changes take longer than the time saved, it's fast enough already.
Re: Hyperlinking output using Perl
by vinoth.ree (Monsignor) on Mar 24, 2014 at 05:36 UTC

    We can do the same in both ways CGI or Tk


    All is well