----- Original Message ----

> From: Aldo Calpini <d...@perl.it>
>
> Ovid wrote:
> > Readonly constants are just easier to use and have fewer "gotchas".
> 
> they have indeed, when you need to access the constants from outside of the 
> module they are declared in (which is a pretty common case).

It also used to be very common not to use strict or warnings.  This doesn't 
mean it's a good thing.  The module in question should provide a sub or method 
to provide access to this data.  Java programmers learned long ago not to let 
people tough their privates, Perl programmers should learn the same thing.  
(For example, when the constant is computed rather than declared, wrapping it 
in a sub saves a *lot* of grief -- if you've done that up front and not forced 
people to change their APIs).

If you *must* use globals, it's certainly a good thing that they're read-only 
but at the very least, do it sanely:

  package Some::Module;
  use Readonly;
  Readonly our $foo => 3;
  use Exporter 'import'
  our @EXPORT_OK = ('$foo'):


Note that we've been forced to switch from 'my' to 'our' in the Readonly 
variable, but now the calling code doesn't need a fully-qualified package name:

  use Some::Module '$foo';
  print $foo;

Cheers,
Ovid
--
Buy the book         - http://www.oreilly.com/catalog/perlhks/
Tech blog            - http://use.perl.org/~Ovid/journal/
Twitter              - http://twitter.com/OvidPerl
Official Perl 6 Wiki - http://www.perlfoundation.org/perl6

Reply via email to