how do i "work out the full url"

update: You only have to do this if HTML::SimpleLinkExtor doesn't do it for you -- and it apparently will do if for you if there is a <BASE HREF> tag in the page that you fetch. For that matter, when you happen to fetch a page that has no such tag, you could just put one in before passing the page to LinkExtor:

s{(</head>)}{<base href="$current_docroot">$1}i;
See below about determining the doc-root from a url. (end of update)

I kinda thought you were already doing that (or trying to) in the code you posted -- but maybe you haven't had a chance to test that part yet (or you did test it, and it didn't work :P).

I'll admit that it's not something I've tried myself, but I think my first impulse would be to do something similar to what you have (this is also not tested):

# $link_target and $url_just_fetched are known... my $full_target; if ( $link_target =~ m{^/} ) { # initial "/" means "relative to doc-root": my $docroot = $url_just_fetched; $docroot =~ s{(?<=[^/]/)[^/].*}{}; # delete everything after first +single slash $full_target = $docroot . $link_target; } elsif ( $link_target !~ /^http/ ) { # it's presumably relative to the url just fetched, so: if ( $url_just_fetched =~ m{/$} ) { $full_target = $url_just_fetched . $link_target; } # this is the tricky part: elsif ( $url_just_fetched =~ /[?&=;:]|\.htm/ ) # probably not a di +rectory name... { my $last_slash = rindex( $url_just_fetched, "/" ) + 1; $full_target = substr( $url_just_fetched, 0, $last_slash ) . $li +nk_target; } else # assume its a directory name { $full_target = join "/", $url_just_fetched, $link_target; } } # (if $link_target does start with "http", then it's probably complete + already) # last step: $full_target =~ s/\#.*//; # in case the link target is a named anchor + within a page
(I did do a quick "super search" for SoPW nodes that discuss "resolve relative href", and only found chromatic saying "there is no such module" -- maybe he was thinking of the question in a different context...)

No doubt there'll be some tweaking to do -- especially the part that tries to guess if the last chunk of a url looks like a directory -- but that should get you started. You probably want to make that a separate subroutine (maybe someday it'll be a module).


In reply to Re: Re: Re: Re: Re: Useless use of substr in void context by graff
in thread Useless use of substr in void context by mkurtis

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.