Quick To The Volcano!

Dmitri Tikhonov

PPW 2014

...before we all come to our senses!

Two Oft-Used Practices

Using Readonly is wrong

1: Readonly variables look like l-values

# This looks like a constant:
ABC;
ABC = 1;    # You'll never write this

Readonly, on the other hand:

if ($ABC) {
    ...
    $ABC = 0;   # Is this readonly?  Scroll up
                # to find out!  PITA.
}

2: Odd way to declare Readonly

Readonly my $RO => 123;
# This compiles, too:
my Readonly $RO => 123;
# And this:
Readonly my $RO = 123;  # no fat comma
    # quick, what's the value of $RO?

3: It's a run-time thing

Readonly my $LOG_LEVEL =>
    $config->get('log_level');

# ...later in the code:

if ($error_condition) {
    ++$LOG_LEVEL;   # Oops! Your application
                    # just crashed at the
                    # worst moment.
    try_to_recover_somehow();
}

4: No tags support for Readonly

Argument against use constant

use constant KB => (1 << 10);
# If you *have* to print it:
print "One kilobyte is still @{[KB]} bytes\n";

A private method in Perl is a bug

Example: superclass

package Super::Class;
sub new {
    ...
    $self->_init;
}
sub _init {
}

Example: subclass

package Sub::Class;
use base qw(Super::Class);
# Unrelated function, not meant to
# override anything:
sub _init {
    do_something_else_now();
}

How to solve it

sub new {
    ...
    _init($self);   # function call now
}

__END__