[tpm] Modification of a read-only value

Shlomi Fish shlomif at iglu.org.il
Fri Apr 23 00:58:07 PDT 2010


On Friday 23 Apr 2010 04:39:31 Shaun Fryer wrote:
> I'm curious why a scalar reference is considered a read-only value?
> 
> $ perl -le 'my $x = \"0"; $$x++; print $$x'
> Modification of a read-only value attempted at -e line 1.
> 
> $ perl -le 'my $x = {x=> 0}; $x->{x}++; print $x->{x}'
> 1
> 
> Is there an easy way to update a simple scalar ref without having to
> use an array or hash ref instead?
> 

Yes:

[code]
#!/usr/bin/perl

use strict;
use warnings;

my $var = 0;
my $x = \$var;

${$x}++;

print "Deref[X] is now: ", ${$x}, ".\n";

[/code]

You may also be able to do something like << my $x = \(my $var = 0); >> but 
please don't for clarity sake.

Regards,

	Shlomi Fish

> Fyi... This is perl, v5.10.0 built for i486-linux-gnu-thread-multi
> --
> Shaun Fryer
> _______________________________________________
> toronto-pm mailing list
> toronto-pm at pm.org
> http://mail.pm.org/mailman/listinfo/toronto-pm

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
Optimising Code for Speed - http://shlom.in/optimise

Deletionists delete Wikipedia articles that they consider lame.
Chuck Norris deletes deletionists whom he considers lame.

Please reply to list if it's a mailing list post - http://shlom.in/reply .


More information about the toronto-pm mailing list