Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Place duplicated code in a subroutine.
Place duplicated subroutines in a module.
The first time you're tempted to copy-paste-and-modify a piece of code:
package Process::Queue;
use Carp;
{
use overload (
# Type coercions don't make sense for process queues...
q{""} => sub {
croak q{Can't stringify a Process::Queue};
},
q{0+} => sub {
croak q{Can't numerify a Process::Queue };
},
q{bool} => sub {
croak q{Can't get the boolean value of a Process::Queue };
},
);
}
# and later...
package Socket;
use Carp;
{
use overload (
# Type coercions don't make sense for sockets...
q{""} => sub {
croak q{Can't convert a Socket to a string};
},
q{0+} => sub {
croak q{Can't convert a Socket to a number};
},
q{bool} => sub {
croak q{Can't get the boolean value of a Socket };
},
);
}