Created
February 15, 2016 09:23
-
-
Save s1037989/e834d72f42a4dd82e6d0 to your computer and use it in GitHub Desktop.
Demonstration of Mojo::Base::tap()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Cat; | |
use Mojo::Base -base; | |
has name => 'Nyan'; | |
has ['age', 'weight'] => 4; | |
package Tiger; | |
use Mojo::Base 'Cat'; | |
has friend => sub { Cat->new }; | |
has stripes => 42; | |
package main; | |
use Mojo::Base -strict; | |
my $mew = Cat->new(name => 'Longcat'); | |
say "\$mew->age = ", $mew->age, " (my age)"; | |
say "\$mew->age(3)->weight(5)->age = ", $mew->age(3)->weight(5)->age, " (my age, after setting my age and weight)"; | |
my $rawr = Tiger->new(stripes => 38, weight => 250); | |
say '---'; | |
say "\$rawr->friend->name('Tacgnol1')->weight = ", | |
$rawr->friend->name('Tacgnol1') # This returns the object at friend | |
->weight, # allowing access to $rawr->friend's methods | |
" (my friend's weight)"; | |
say "\$rawr->tap(sub { \$_->friend->name('Tacgnol2') })->weight = ", | |
$rawr->tap(sub { $_->friend->name('Tacgnol2') }) # This returns the object at $rawr | |
->weight, # allowing access to $rawr's methods | |
" (my weight, after setting my friend's name)"; | |
say "\$rawr->friend->name = ", $rawr->friend->name, # Notice the name has been changed, so it's not a temporary change | |
" (my friend's name)"; | |
__END__ | |
$mew->age = 4 (my age) | |
$mew->age(3)->weight(5)->age = 3 (my age, after setting my age and weight) | |
--- | |
$rawr->friend->name('Tacgnol1')->weight = 4 (my friend's weight) | |
$rawr->tap(sub { $_->friend->name('Tacgnol2') })->weight = 250 (my weight, after setting my friend's name) | |
$rawr->friend->name = Tacgnol2 (my friend's name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment