in reply to Re^9: How to safely use $_ in a library function?
in thread How to safely use $_ in a library function?

Yes, Internals::SvREADONLY(..., 1) works to correctly make a writeable variable read-only:

#! perl use strict; use warnings; my $s = 'fred'; print "\$ = $s\n"; $s = 'barney'; print "\$s = $s\n"; Internals::SvREADONLY($s, 1); $s = 'wilma'; print "\$ = $s\n";

Output:

19:04 >perl 723_SoPW.pl $s = fred $s = barney Modification of a read-only value attempted at 723_SoPW.pl line 10. 19:07 >

And Internals::SvREADONLY(..., 0) then works to make the variable writeable again:

#! perl use strict; use warnings; my $s = 'fred'; print "\$s = $s\n"; $s = 'barney'; print "\$ = $s\n"; Internals::SvREADONLY($s, 1); Internals::SvREADONLY($s, 0); $s = 'wilma'; print "\$ = $s\n";

Output:

19:10 >perl 723_SoPW.pl $s = fred $s = barney $s = wilma 19:12 >

But it doesn’t work on a variable which is aliased to a literal:

#! perl use strict; use warnings; for my $s ('fred') { print "\$s = $s\n"; Internals::SvREADONLY($s, 0); $s = 'wilma'; print "\$ = $s\n"; }

Output:

19:14 >perl 723_SoPW.pl $s = fred Modification of a read-only value attempted at 723_SoPW.pl line 9. 19:16 >

So Internals::SvREADONLY() “works” under Windows; but (it would appear) its effect on an aliased variable is not portable?

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,