Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^3: OOP - Constant Vs. Subroutine

by diotalevi (Canon)
on May 13, 2007 at 04:01 UTC ( [id://615141]=note: print w/replies, xml ) Need Help??


in reply to Re^2: OOP - Constant Vs. Subroutine
in thread OOP - Constant Vs. Subroutine

Oh?

use constant FOO => { x => 0 }; print FOO->{x}; # 0 ++ FOO->{x}; print FOO->{x}; # 1

Readonly also makes sure your entire data structure is readonly. constant only does that to the topmost level.

⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Replies are listed 'Best First'.
Re^4: OOP - Constant Vs. Subroutine
by BrowserUk (Patriarch) on May 13, 2007 at 05:34 UTC

    You can achieve the same effect as Readonly for hashes, without the overhead, using Hash::Util:

    #! perl -slw use strict; use Hash::Util qw[ lock_hash ]; use constant FOO => { x => 0 }; lock_hash( %{ FOO() } ); ++ FOO->{x}; print FOO->{x}; __END__ C:\test>junk7 Modification of a read-only value attempted at C:\test\junk7.pl line 7 +.

    It would be a nice addition to constant if it would (or could be instructed to) do that for you.

    Internals::SetReadOnly(), also works to an extent on both hashes and arrays:

    #! perl -slw use strict; use Internals qw[ SetReadOnly ]; use constant BAR => { x => 1 }; SetReadOnly( BAR ); BAR->{y} = 12345; ## Modification of a read-only value attempted ... use constant BAZ => [ 1, 2, 3, 4, 5, ]; SetReadOnly( BAZ ); push @{ +BAZ }, 1; # Modification of a read-only value attempted ... ## Unfortunately, these do not produce errors? ++BAR->{x}; ++BAZ->[0]; __END__ C:\test>junk7 Modification of a read-only value attempted at C:\test\junk7.pl line 1 +3.

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      I frequently have constant data structures. Having to loop over all the data in my constant just to set the readonly flag sucks and mostly I don't do it. I'd really just wish for constant.pm to learn to do the right thing. I suppose I should just go patch it.

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re^4: OOP - Constant Vs. Subroutine
by naikonta (Curate) on May 13, 2007 at 05:50 UTC
    Well, constant does say,
    Even though a reference may be declared as a constant, the reference may point to data which may be changed
    Again, for my personal need, 99% of my constants are simple scalars. Update: But then, most of my codes don't use constants at all. Just to be clear, I'm not by any means in position againsts Readonly. My first reaction to it was, "OK, cool, I'll might use it someday".

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://615141]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (1)
As of 2024-04-24 13:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found