jeroenes has asked for the wisdom of the Perl Monks concerning the following question:
In short, a function that does a SWITCH:{$_ = .... get's an error 'Modification of a read-only value attempted' when when two function calls back a $_ was passed as the first parameter. It's getting lengthy here, so....
Let's start with the last function. After two hard-to-debug errors, I decided to write a module that checks whether a method was called with a $self->. This module is exports the function selfcontrol and contains the following code:
Normally, this works. I call this function in the first line of a method this way:package Selfcontrol; use strict; use Exporter; use vars qw( @EXPORT @ISA @VERSION); @VERSION = 0.01; @ISA = qw( Exporter ); @EXPORT = qw( &selfcontrol); use Carp; sub selfcontrol{ my $self = shift; my $ref = ref( $self); SWITCH: { $_ = $ref; last unless $_; last if /REF/; last if /SCALAR/; last if /ARRAY/; last if /HASH/; last if /CODE/; last if /GLOB/; return $self; } confess 'You forget the $self->, you idiot!'. "\n\tInstead you gave me $self that refs to '$ref'"; }
because I fear the selfcontrol will slow things up when it's called too often, and I have a feeling that using modules to find out the caller would not greatly improve on speed. $bugfree is a global scalar declared with use vars.my $self = $bugfree ? shift : selfcontrol( shift );
Well, somewhere in my actual class (well, at line 300 of over 1100) I use a for loop:
The findTokenPos first does the selfcontrol magic, and it gets beaten like hell:for ('Operator', 'SQLStatement', 'PerlVariable', 'CompModifier +') { $self->findTokenPos( $_, $i, $blocklast ); $numberOthers += scalar( @_ ); }
This is my workaround:# # Modification of a read-only value attempted, chunk 1. File ':Selfcontrol.pm'; Line 15 # Selfcontrol::selfcontrol('embedSQL=HASH(0x2b31864)') called File ':embedSQL.pm'; Line 876 # embedSQL::findTokenPos('embedSQL=HASH(0x2b31864)', 'Operator', 2 +, 3) called File ':embedSQL.pm'; Line 292 # embedSQL::synonymsSELECT('embedSQL=HASH(0x2b31864)') called File ':embedSQL.pm'; Line 202 # embedSQL::parse('embedSQL=HASH(0x2b31864)') called File 'Documenten:embedSQL:test1.pl'; Line 10
And hey, no errors any more! Could anyone please telling me why the first for loop doesn't work? Thanks a lot!for my $str ('Operator', 'SQLStatement', 'PerlVariable', 'Comp +Modifier') { $self->findTokenPos( $str, $i, $blocklast ); $numberOthers += scalar( @_ ); }
Jeroen
I was dreaming of guitarnotes that would irritate an executive kind of guy (FZ)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Readonly error on $_
by merlyn (Sage) on Jan 05, 2001 at 20:37 UTC | |
by jeroenes (Priest) on Jan 06, 2001 at 02:02 UTC | |
by tye (Sage) on Jan 11, 2001 at 19:38 UTC |