Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Help with cause of "Modification of a read-only value attempted at ..."

by shriken (Priest)
on Sep 28, 2010 at 14:07 UTC ( [id://862420]=perlquestion: print w/replies, xml ) Need Help??

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

I have read over Common Causes for "Modification of a read-only value attempted" but am (obviously) still not seeing my mistake. I have some dusty Perl code that was being run with 5.8.8, and today I tried running it under 5.10.1. Execution stopped early on with:

Modification of a read-only value attempted at /usr/local/emonitor/perl_libs/emonitor.pm line 190.

That module has (among other things)...

package emonitor; require Exporter; # snip for post brevity use strict; # more snipping of various other modules being use'd our @ISA = qw(Exporter); our @EXPORT = qw(); our @EXPORT_OK = qw( # still more snipping of other methods etc sub list_hosts { my($cfg, $hosts_r, $ow) = @_; bless($cfg, 'configthing'); ## <-- this is line 190

In the main file, the emonitor modules is use'd as use emonitor qw() and the call to the method happens like this (again, with liberal snippage):

use strict; use warnings; use configthing qw(); our configthing $cfg = new configthing; emonitor::list_hosts($cfg, \@hosts);

Replies are listed 'Best First'.
Re: Help with cause of "Modification of a read-only value attempted at ..."
by Corion (Patriarch) on Sep 28, 2010 at 14:11 UTC
    our configthing $cfg = new configthing;

    You don't show us what your routine configthing::new looks like. This is where $cfg would get set. Maybe Data::Dumper can tell you more:

    ... use Data::Dumper; our $cfg = configthing->new(); print Dumper $cfg; emonitor::list_hosts($cfg, \@hosts);

    I really, really wonder though, why you would need to (re)bless $cfg into configthing in the package emonitor.

      package configthing; use strict; use Cwd 'getcwd'; use File::Basename; use fields qw( # snipped for brevity... ); our %DEFAULTS = ( # snipped for brevity... ); 1; sub new { my $this = shift; unless (ref $this) { $this = fields::new($this); } # properties (keys in the pseudohash) we set when an # object is initialized $this->{pname} = basename($0); $this->{pdir} = dirname($0); if ( exists($ENV{'TERM'}) && '' ne $ENV{'TERM'} ) { $this->{is_interactive} = 1; } else { $this->{is_interactive} = 0; } # define empty values for all known configuration keys our %FIELDS; foreach my $k ( keys(%FIELDS) ) { $this->{$k} = '' unless defined($this->{$k}); } # initialize default values our %DEFAULTS; foreach my $k ( keys(%DEFAULTS) ) { $this->{$k} = _expand_value($this, $DEFAULTS{$k}); } return $this; }

      doh! I'm mising the bless($this, 'configthing'); which explains why I put 33 separate bless($cfg) incantations all over the place. Dusty code that I obviously didn't understand when I wrote it.

        I don't see anything immediately wrong in relation to the error at hand. Please do as JavaFan suggests and post a small program that reproduces the error.

        You really, really should look over this code still, because it (re)declares our %DEFAULTS in two places which makes very little sense as %DEFAULTS is still visible from above.

Re: Help with cause of "Modification of a read-only value attempted at ..."
by Anonymous Monk on Sep 28, 2010 at 14:12 UTC

    The first thing you should do is show what print Dumper $cfg; says inside your list_hosts function.

    If in doubt, print it out!

Re: Help with cause of "Modification of a read-only value attempted at ..."
by shriken (Priest) on Sep 28, 2010 at 16:03 UTC

    This seems to be working correctly, and properly coded now. I've gone through and...

    - Removed all the extraneous bless($cfg,'configthing') statements (eg, the line 190 in the original error message) that were scattered all over the original code.

    - Added require 5.10.0; statements to the various main files to prevent inadvertently going back to 5.8.8 (which is the OS's perl installation.)

    As Corion pointed out, this thing needs a review . . .

Re: Help with cause of "Modification of a read-only value attempted at ..."
by JavaFan (Canon) on Sep 28, 2010 at 14:23 UTC
    I cannot reproduce that. Could you gives us a small, stand-alone program that gives this error message?

      Trimming down, even before I have a small case that shows the error I have something else wrong. Here're two files that give me different results on 5.8.8 versus 5.10. I think I've already done something wrong at this level of simplicity that I should figure out before building the example up farther.

      use strict; use warnings; use configthing qw(); our configthing $cfg = new configthing; use Data::Dumper; print Dumper $cfg;

      Then for "configthing.pm"...

      package configthing; use strict; use warnings; use fields qw( a b c d ); our %DEFAULTS = ( b => 'default for b', c => 'default for c' ); 1; sub new { my $this = shift; unless (ref $this) { $this = fields::new($this); } our %FIELDS; foreach my $k ( keys(%FIELDS) ) { $this->{$k} = '' unless defined($this->{$k}); } $this->{a} = 'set at initialization'; foreach my $k ( keys(%DEFAULTS) ) { $this->{$k} = $DEFAULTS{$k}; } return $this; }

      With 5.8.8 I get

      $VAR1 = bless( [ bless( { 'c' => 3, 'a' => 1, 'b' => 2, 'd' => 4 }, 'pseudohash' ), 'set at initialization', 'default for b', 'default for c', '' ], 'configthing' );

      And 5.10.1 gives me:

      $VAR1 = bless( { 'c' => 'default for c', 'a' => 'set at initialization', 'b' => 'default for b', 'd' => '' }, 'configthing' );

        This is completely to be expected in a change from 5.8 to 5.10 because pseudohashes do not exist anymore in 5.10. The implementation of fields was changed to shield you from this other change I believe.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://862420]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-19 23:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found