One way of doing it would be defining a hash whose keys are the keywords (the ones you want to provide links for) -- this will work with phrases too, BTW -- and whose values are URIs or URLs. Then just use s///g to do it (that's the quick n' dirty way).

#!/usr/bin/perl -w use strict; my %keywords = ( foo=>'/foo.html', bar=>'bar.html', cult=>'http://perlmonks.org' ); my $textfile ="/file/to/link"; open TEXTFILE, $textfile or die "Yikes! $textfile won't open: $!\n"; my $data; { local $/; #sets input list separator to undef #so we can 'slurp' the file in $data = <TEXTFILE>; } close TEXTFILE; foreach (keys %keywords) { my $url = $keywords{$_}; # EDITED ... I'd forgotten to escape # the / in front of the closing <a> # credit to Albannach $data =~ s/($_)/<a href="$url">$1<\/a>/g; } # now do something with $data

But that's *oh so quick* and *oh so dirty*, it probably raises a lot of problems and only works on very simple kinds of words (e.g. if you have phrases in your keywords hash, you could really mess things up).

UPDATE Implementing something like this site's linking mechanism wouldn't be so hard. just put delimiters around the words / phrases you want to link, and something like the above should work fairly well. E.g. [bob] would say 'put a link around "bob"' (what link, you could determine in a number of ways, but the hash idea seems to work well enough.)

Just alter the above substitution line to:

$data =~ s/\[($_)]<a href="$url">$1<\/a>/g;

Bonus: that takes care of phrases, too. (credit for this idea goes to whathisname

Seems like there must be a module that does something like this (I'll go to CPAN and give you an update)

Philosophy can be made out of anything. Or less -- Jerry A. Fodor


In reply to Re: Auto linking to words in a text file by arturo
in thread Auto linking to words in a text file by belize

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.