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

hey, this is probably pretty remedial, but i'm having a lot of trouble with it.
i'm using a regex to find any data in a page that starts with

/http:\/\//

and trying to convert it to a link. i can locate the text just fine, but i'm having trouble converting it to an A HREF using CGI.pm.
any advice would help a lot!!

Replies are listed 'Best First'.
Re: changing existing text to a link
by Aristotle (Chancellor) on May 04, 2003 at 06:48 UTC
(jeffa) Re: changing existing text to a link
by jeffa (Bishop) on May 04, 2003 at 14:05 UTC
    Using the advice of Aristotle (and, no offense intended, ignoring the advice of Abigail-II (not generally recommended)), here is some code for you. (chalk it up to a lazy Sunday ;))
    use strict; use warnings; use CGI qw(a); use URI::Find::Schemeless; my $text = do {local $/;<DATA>}; my $finder = URI::Find::Schemeless->new( sub { return a{href=>$_[0]->abs},$_[1] } ); $finder->find(\$text); print $text; __DATA__ stuff stuff http://foo.com/bar/qux.html stuff stuff stuff http://bar.com/baz.cgi?foo=bar stuff stuff stuff www.perlmonks.org/?node_id=255439
    Personally, i love using CGI.pm to mark up text with HTML. Sure it may be an elephant, but it's a tried, true, and tested elephant! ;)

    CORRECTION: I realized that I had the arguments to CGI::a wrong - i swapped $_[0] and $_1 to correct the mistake.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: changing existing text to a link
by Juerd (Abbot) on May 04, 2003 at 10:56 UTC

    s[((\w+://|www\.|WWW\.)[a-zA-Z0-9\.\@:-]+[^\"\'>< \r\t\n]*)] { local $_ = $1; my $scheme = $2; s/// if (my $trailing) = /([.,!?()\[\]]+$)/; s/&(?!\x23?\w+;)/&amp;/g; s/\"/&quot;/g; my $href = ($scheme =~ /www\./i ? "http://$_" : $_); qq{<a href="$href" target="_blank">$_</a>$trailing}; }eg;
    is what I use.

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

Re: changing existing text to a link
by Abigail-II (Bishop) on May 04, 2003 at 09:56 UTC
    Why would you want to use CGI.pm to modify text? You already find the data you need with a regex, so, why don't you just use a substitution? You already did the hardest part.

    Using CGI.pm to do substitutions is like using an elephant to write a letter.

    Abigail

      The NelliePen Society

      J.P. Snodgrass. President.

      To whom it may concern.

      We wish to protest in the strongest terms, this flagrant, un-called for, and clearly prejudicial use of supposedly humourous analogies with total disregard for those ....

      "Come back Nellie.....what is the matte....".

      "Ellie? Ellie! You didn't let Nellie have the left over spinach did you?........."

      "Oh dear"..


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
Re: changing existing text to a link
by The Mad Hatter (Priest) on May 04, 2003 at 16:18 UTC
    If capturing the link text after finding it so you can use it with the a method in CGI, then just use capturing parentheses in the regex.
    $_ = 'some text http://moo.com fish'; /(http:\/\/)/ $href = $1;
    For each pair of capturing parentheses (up to nine total), the text they capture is put into $1 through $9. Read perldoc perlre for more information about this and other regex stuff.

    Of course, I'd recommend using some of the excellant solutions in this thread, but this is up to you.

Re: changing existing text to a link
by Jaap (Curate) on May 04, 2003 at 09:30 UTC
    How are you having trouble converting it using CGI? Please exlain further. You could also post a piece of code to make the problem clear.