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

Hi, is there an easy way to insert text between a string? I want to do something like this;

href=www.perlmonks.org and I want to insert the http:// so the outcome + would be; href=http://www.perlmonks.org.
Cheers
Chris

Replies are listed 'Best First'.
Re: Insert text in a string
by little (Curate) on Jan 18, 2002 at 14:50 UTC
    I hope its a typo, but you have invalid HTML there :-)
    That should be href="http://www.perlmonks.org"and so I suggest using HTML::Parser or HTML::TokeParser to find all anchor tags and then determine whether they have an invalid argument value, eg. that wrong syntax with missing " and omitted protocol.
    And yes, the links go to perl monks search pages as this is the easy way to learn from others expieriences and at the bottom of those search pages you can search CPAN where you find the modules and their manpages :-)

    Have a nice day
    All decision is left to your taste
Re: Insert text in a string
by metadoktor (Hermit) on Jan 18, 2002 at 14:29 UTC
    Since you already know what position you want to insert in this string, you could use the following fragment:

    $_="href=www.perlmonks.org"; substr($_,5,0)="http://";

    metadoktor

    "The doktor is in."

      I should have mentioned that I have to do this for a html page full of links and there could be more than one of these links per line. Would I use something similar again?

      Cheers
      Chris

Re: Insert text in a string
by giulienk (Curate) on Jan 18, 2002 at 15:37 UTC
    You could also take a regexp approach:
    $_ = 'href=www.perlmonks.org'; s!href=!href=http://!gi;

    $|=$_="1g2i1u1l2i4e2n0k",map{print"\7",chop;select$,,$,,$,,$_/7}m{..}g

      At the very least, you should use a look-ahead to avoid prefixing 'http://' to strings that already have one...
      s|href=(?!http://)|href=http://|gi;

      -Blake