Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: Modification of a read-only value attempted

by frozenwithjoy (Priest)
on Nov 27, 2012 at 16:50 UTC ( #1005882=note: print w/replies, xml ) Need Help??

in reply to Modification of a read-only value attempted

I've never taken this approach when making a read-only variable (opting for using constant/Readonly, instead).

Question for other monks: How common/acceptable is an approach like OP's $c=\99;? Although concise, it is less readable (and probably easy to not notice). Thanks.

Replies are listed 'Best First'.
Re^2: Modification of a read-only value attempted
by choroba (Chancellor) on Nov 27, 2012 at 16:59 UTC
    This does not create a read-only variable, but a read-only value.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Oh, because you can actually assign something else to $c. Of course! Thanks
Re^2: Modification of a read-only value attempted
by mbethke (Hermit) on Nov 27, 2012 at 17:03 UTC

    You will notice when you try to write to the constant, which I suppose is the point of it :) That said, I do prefer Readonly as well because it allows for completely normal-looking "non-variables". Having to remember to dereference anything that's supposed to be a constant doesn't make sense except as an idiosyncrasy of the language. What's more, Readonly also lets you write-protect hashes and arrays although at a performance cost.

      This discussion reminds me of a related question. When a Readonly hash or array contains references, are the targets of those references protected? As a common example, are the elements of Readonly matrix protected?


        Easy to test:

        >perl -wMstrict -MData::Dump -le "use Readonly; ;; Readonly my %h => (a => {b => {c => {d => 1}}}); dd \%h; ;; $h{a}{b}{d} = 2; " { # tied Readonly::Hash a => { # tied Readonly::Hash b => { # tied Readonly::Hash c => { # tied Readonly::Hash d => 1, }, }, }, } Modification of a read-only value attempted at -e line 1

        See also DESCRIPTION section of Readonly.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1005882]
[Cosmic37]: I guess it is searching for the string "$mydt"
[Corion]: Indeed cool, erix ;)
[Cosmic37]: rather than the value of $mydt which is a date time strong such as 2016-01-01 12:30:56
[Corion]: Cosmic37: No, but maybe $mydt doesn't contain what you think it does, or it contains characters that are special in a regular expression? Try if( $line =~ /\Q$mydt\E/) { ... for a literal match
[Cosmic37]: I mean string grrr
[Corion]: Maybe add an else branch in which you print what the values of $line and $mydt are?
[Cosmic37]: ah thank you I will try
[Cosmic37]: you are right $mydt did not contain what I expected... :-D I will now think :-D
[Cosmic37]: crunch...grind... whirr...gnashing metal crunching cogs...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2017-06-29 17:04 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (673 votes). Check out past polls.