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

I do this:

use LWP::Simple; $asdf = get("http://asdf.org"); $asdf =~ s/asdf//g;

And I get this error:
Modification of a read-only value attempted . . .

What gives? I tried using the getstore method, but I got the same error. I can do matches (reading) but not substitutions (writing). How can I make it a read/write value?

Replies are listed 'Best First'.
Re: LWP::Simple and Read-only Values
by Zaxo (Archbishop) on Jul 13, 2003 at 02:30 UTC

    I suspect that your request is timing out or failing for some other reason. Your error message is emitted when an attempt is made to modify undef, among other causes. Your code worked for me. Try testing the result of the get() call like so,

    defined( my $asdf = get 'http://asdf.org') or die 'LWP::get http://asdl.org failed: ', $!;
    If you need to know why a request is failing, use the full-bore LWP so you can check the response headers.

    After Compline,
    Zaxo

Re: LWP::Simple and Read-only Values
by bobn (Chaplain) on Jul 13, 2003 at 02:26 UTC

    I don't get this error. Are you sure the code you posted is the code that is causing the error?

    --Bob Niederman, http://bob-n.com
      No, it is not the exact same code. The exact code includes this:
      $asdf =~ /(asdf)/; $1 =~ s/asdf//;

      In other words, I was trying to modify the special variable $1. Once I set $1 to something else, the code worked like a charm. Why can't I do substitutions on $1?

        You can't modify $1 because it's a read only variable. It doesn't make sense to make it a writeable variable because it's only ever supposed to contain the first capture from the most recently successful match.

        That may not be the most convincing explanation, but that's the official reason. If you could modify the magic global captures, should or shouldn't it affect the matched string itself? That's pretty tricky and, pragmatically, it's a stone better left unturned.

        3rd edition of Camel flags these ($1, $2, etc.) clearly as readonly, as does perldoc perlvar

        --Bob Niederman, http://bob-n.com