[PBP-pm] Named constants from modules?

C. Garrett Goebel ggoebel at goebel.ws
Sun Nov 20 07:08:12 PST 2005


On Fri, 18 Nov 2005, Stuart Jefferys wrote:

> This is my first post here. Hope it works...

welcome.


> The Conway PBP book recommends using named constants in place of
> magic numbers.
...
> However, the book also recommends against allowing export
> of any variables from modules.

True, but scalars, arrays and hashes created with Readonly aren't
variable variables.

So the interface variable prohibition on making variables part
of a modules interface wouldn't really apply. The reasoning behind that
prohibition is that you want to control exposure of a module's
internal state, access to that state, and how it can be changed.

None of those reasons apply to the non-modifiable "variables" constructed
with Readonly.


In your wishlist request, you wrote:
> For instance, if I have a module of physics constants, and want to use
> them by name in a calling module.

Which sounds reasonable.


> Also, I have never tried it, but it just occurred to me -- what
> happens upon export of "Readonly" tagged variables anyway?

    package Foo;
    use Readonly;
    use base qw(Exporter);
    use vars qw($FOO);

    our @EXPORT = qw($FOO);
    Readonly $FOO = 3.14;

    package main;
    Foo->import();
    print qq{$FOO\n};"
    $FOO=1;

results in:
    3.14
    Modification of a read-only value attempted at ...

cheers,

Garrett



More information about the PBP-pm mailing list