in reply to OOP - Constant Vs. Subroutine

Isn't Best Practice to use Readonly not constant?


Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality

Replies are listed 'Best First'.
Re^2: OOP - Constant Vs. Subroutine
by BrowserUk (Patriarch) on May 12, 2007 at 15:55 UTC

    You pays your money and takes your choice:

    #! perl -slw use strict; use Benchmark qw[ cmpthese ]; use constant { X => 0, Y => 1, Z => 2, P => 3, Q => 4, }; use Readonly; Readonly::Hash my %Fields => { X => 0, Y => 1, Z => 2, P => 3, Q => 4, }; sub prioritisedSort1 { return $a->[ P ] <=> $b->[ P ] or $a->[ Z ] <=> $b->[ Z ] or $a->[ Y ] <=> $b->[ Y ] or $a->[ X ] <=> $b->[ X ] or $a->[ Q ] <=> $b->[ Q ]; } sub prioritisedSort2 { return $a->[$Fields{P}] <=> $b->[$Fields{P}] or $a->[$Fields{Z}] <=> $b->[$Fields{Z}] or $a->[$Fields{Y}] <=> $b->[$Fields{Y}] or $a->[$Fields{X}] <=> $b->[$Fields{X}] or $a->[$Fields{Q}] <=> $b->[$Fields{Q}]; } our $N ||= 1e5; my @AoA1 = map { [ map{ int rand $_ } 1000, 100, 10, 500, 50 ] } 1 .. $N; my @AoA2 = map{ [ @$_ ] } @AoA1; my( @sorted1, @sorted2 ); cmpthese -1, { constant => sub{ @sorted1 = sort prioritisedSort1 @AoA1 }, Readonly => sub{ @sorted2 = sort prioritisedSort2 @AoA2 }, }; print "\nFirst and last 5 for constant"; print "[@$_]" for @sorted1[ 0 .. 4 ], [ qw[ - - - - - ] ], @sorted1[ -5 .. -1 ]; print "\nFirst and last 5 for Readonly"; print "[@$_]" for @sorted2[ 0 .. 4 ], [ qw[ - - - - - ] ], @sorted2[ -5 .. -1 ]; __END__ C:\test>junk8 -N=1e2 Rate Readonly constant Readonly 137/s -- -96% constant 3459/s 2427% -- First and last 5 for constant [226 44 0 8 3] [425 64 7 10 2] [790 25 8 20 27] [461 37 0 27 47] [151 51 3 31 35] [- - - - -] [734 15 9 482 6] [90 40 2 489 5] [957 21 9 491 45] [17 99 3 494 44] [585 50 7 495 22] First and last 5 for Readonly [226 44 0 8 3] [425 64 7 10 2] [790 25 8 20 27] [461 37 0 27 47] [151 51 3 31 35] [- - - - -] [734 15 9 482 6] [90 40 2 489 5] [957 21 9 491 45] [17 99 3 494 44] [585 50 7 495 22]

    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.
Re^2: OOP - Constant Vs. Subroutine
by naikonta (Curate) on May 12, 2007 at 03:34 UTC
    Yes, per TheDamian advice. And see also merlyn's article. However, for my personal need, I don't see any advantages other than interpolating purpose.

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

      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.

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

        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.
        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!

Re^2: OOP - Constant Vs. Subroutine
by 2xlp (Sexton) on May 12, 2007 at 22:40 UTC

    I should have clarified my intent...

    If you're looking for 'read only' variables, then yes-- use Readonly , not constant

    AFAIK : Readonly creates package variables , while constant creates inheritable subroutines. For my needs, constant was more appropriate -- though a sub with a null prototype seems to be even more appropriate.