NAME
Sub::Mage::Spellbook - Spells and incantations for Sub::Mage
INCANTATIONS
We'll start with incantations seems that they are summoned at the beginning when we use Sub::Mage
. Generally they will be in the form of an array, as such:
use Sub::Mage qw/:5.010 :Debug :Moose/;
The above is a perfectly good incantation. The first option :5.010
imports the 5.010 feature (so you can use "say" and "given"), the second :Debug
turns on debugging, and the last one we used :Moose
lets Sub::Mage know that you're using Moose, so don't import overide
, after
and before
as Moose already has these features. Another handy incantation is :Class
. Please read below about telekinesis to see how it works.
Telekinesis
This is the action of controlling a class from a different class or script. Sub::Mage makes this easy with its spells.
First, let's create a blank package and turn it into our class. When using :Class
it removes the need to add sub new { ... }
.
package Spells;
use Sub::Mage qw/:Class/;
1;
That's our whole class.. now, let's control it from merlin.pl
use Spells;
my $spells = Spells->new;
Spells->conjur( fireball => sub {
my $self = shift;
$self->{damage} = 5;
});
Spells->conjur( damage_of_fireball => sub {
return shift->{damage};
});
$spells->fireball;
print $spells->damage_of_fireball; # returns 5
Notice how we didn't even need to load Sub::Mage
in merlin.pl
? This is because we used everything from the Spells
class. We've created the subroutine fireball
but we want to let people create their own damage modifier and make it verbose..
Spells->override( fireball => sub {
my ($self, $val) = @_;
$self->{damage} = $val;
});
Oh, we forgot to make it verbose. Let's just tag it on the end...
Spells->after( fireball => sub {
my ($self, $val) = @_;
print "Damage set to $val\n";
});
So now we call..
$spells->fireball(10);
And see Damage set to 10
. And that's a basic Telekinesis spell.