Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: Style: buried variables or double referencing?

by tilly (Archbishop)
on Aug 20, 2005 at 18:12 UTC ( [id://485410]=note: print w/replies, xml ) Need Help??


in reply to Re: Style: buried variables or double referencing?
in thread Style: buried variables or double referencing?

TheDamian's Best Practices gives several good reasons why constant should be deprecated in favour of Readonly. In which case you would have:
use Readonly; Readonly my $LONG_COUNT => 14; Readonly my $SHORT_COUNT => 9; # ...
I fully agree with his recommend since I never saw that constant buys me anything useful.

Replies are listed 'Best First'.
Re^3: Style: buried variables or double referencing?
by pg (Canon) on Aug 20, 2005 at 18:59 UTC

    Depends. Readonly is slow, and not everyone can use Readonly::XS. The following code takes 48 seconds:

    use Readonly; Readonly $C1 => 1; my $t0 = time(); my $a = 2; for (1 .. 10000000) { if ($a ++ == $C1) { ; } } print time() - $t0;

    When this takes 4 second:

    use constant C1 => 1; my $t0 = time(); my $a = 2; for (1 .. 10000000) { if ($a ++ == C1) { ; } } print time() - $t0;

    If performance is important, use constant.

      I'd be happier developing with a function that called Readonly in development and then something like this in production:
      sub fake_readonly { $_[0] = $_[1]; }
      And now you get most of the advantages of Readonly, but performance is not a problem. (I don't like constant because I'm a big fan of interpolating, and I often have to check whether someone has a constant or function because I'm afraid that it will try to suck up arguments.)
      That's a difference of 4.4 microseconds. That's a difference I'm willing to take in most of my programs.
Re^3: Style: buried variables or double referencing?
by Smylers (Pilgrim) on Aug 22, 2005 at 10:02 UTC

    TheDamian's <cite>Best Practices</cite> gives several good reasons why constant should be deprecated in favour of Readonly:

    Readonly my $LONG_COUNT => 14; Readonly my $SHORT_COUNT => 9;

    But Damian also says in his book that you should use hashes for look-up tables, rather than if/else tests. And that's definitely a look-up table.

    So how about something like:

    Readonly \my %THWAPP_COUNT => ( x => 14, y => 9, );

    (Adjective describing what sort of a count this is courtesy of Acme::MetaSyntactic::batman.)

    Smylers

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-25 09:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found