Can anyone explain this read-only exception?

Below is my code (don't worry that USUW is at the top of the module)

I am testing if the ref array is readonly, and if so, I copy it to another ref array. Tests show that the array is not read-only, but when it is executed, it does not work with this error. (For those of you who are not familiar with me or Smart::Comments--those ### Smart::Comments.)

### readonly( $arg_ref ) : readonly( $arg_ref )
### readonly( @$arg_ref ) : readonly( @$arg_ref )
my @ro = map { readonly( $_ ) } @$arg_ref;
### @ro
if ( readonly $arg_ref ) {
    $arg_ref = [ @$arg_ref ];
}
return map { my $val = shift @$arg_ref;
             $_ => $val 
            } @_ 
            ;

This is the result I get:

### readonly( $arg_ref ) : 0
### readonly( @$arg_ref ) : 0

### @ro: [
###        0,
###        0,
###        0,
###        0,
###        0
###      ]

But here is the error:

Modification of a read-only value attempted at ....pm line 247.

(247:

return map { my $val = shift @$arg_ref;

)

Is anyone familiar with this problem? We are launching Perl 5.8.7. Any idea on how to solve it?

+5
source share
4 answers

Scalar:: Util:: readonly trusted used . :

perl -MScalar::Util=readonly -MReadonly -wle'
    Readonly my $arg_ref => [ qw(a b c)];
    print readonly $arg_ref;
    $arg_ref = 1;'

:

0
 Modification of a read-only value attempted at -e line 1.

( perl5.8.8 Readonly 1.03, Scalar:: Util 1.23)

0

DBI::fetchrow_arrayref , : , (.. ).

, , ,

my $arg_ref = [ $sth->fetchrow_array ];
+2

Readonly readonly . ( R r.;-))

Scalar::Util, :

readonly SCALAR

true, SCALAR .

sub foo { readonly($_[0]) }
$readonly = foo($bar);              # false
$readonly = foo(0);                 # true

( , foreach @_, Readonly.

+1

, . , : Readonly Readonly , , , . , .

Readonly : (Readonly::XS), SvREADONLY ( Readonly:: Scalar) , SvREADONLY.

Readonly : Perl ( , ), XS ( SvREADONLY).

4 , , , ( Perl Readonly XS ) , , .

crux: sub Readonly::Readonly Readonly::XS, sub Readonly::Scalar . , . Readonly , readonly: readonlyness .

Readonly, , , , , . , .

+1

All Articles