in reply to Re: Export Constants Common to Lots of Packages
in thread Export Constants Common to Lots of Packages

I don't use constant because of p. 55 in Perl Best Practices, authored by TheDamian. It recommends not using use constant at all, but the Readonly module instead. Since I can also install Readonly:XS, I don't have to worry about this module being slow with my scalars.

Replies are listed 'Best First'.
Re^3: Export Constants Common to Lots of Packages
by Ovid (Cardinal) on Nov 29, 2005 at 20:42 UTC

    Same here. At work, we've recently removed "use constant" from our code and converted to Readonly due to Damian's advice. It's really cleaned up a lot of our code.

    Unfortunately, there are some bugs in how Perl handle tie which Readonly exposes. For example, the following prints "no":

    #!/usr/bin/perl -l use strict; use warnings; use Readonly; Readonly my $VAR => 'SEARCH'; foreach my $var ($VAR) { print +($var eq $VAR) ? 'yes' : 'no'; }

    Clearly that's wrong, but it's related to Perl's tie mechanism and not Readonly. As a workaround:

     print +($var.'' eq $VAR.'') ? 'yes' : 'no';

    That's ugly as sin, but it works :(

    Cheers,
    Ovid

    New address of my CGI Course.

      Would "$var" eq "$VAR" work?

        I just tested that. Yes, it works. Much nicer, too. Thanks.

        Cheers,
        Ovid

        New address of my CGI Course.

Re^3: Export Constants Common to Lots of Packages
by ikegami (Patriarch) on Nov 29, 2005 at 21:33 UTC

    Constants are different than normal variables, so I like how use constant constants look differently (due to lack of a sigil). Isn't that one of Perl's mantras?

    Readonly uses tie. Tied variables are slower, and they behave erratically in a few situations.

    On the other hand, Readonly works on types other than scalar.