diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2016-04-13 14:21:41 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2016-04-13 14:21:41 +1200 |
commit | cebc43339efa6e1ee7f016e3d53aba821ab301f5 (patch) | |
tree | 44d4698cf2a0746470798943808b7dfdf466564c | |
download | perlobj-demo-cebc43339efa6e1ee7f016e3d53aba821ab301f5.tar.gz perlobj-demo-cebc43339efa6e1ee7f016e3d53aba821ab301f5.zip |
First commit
Just demonstrating Perl's oddish object-oriented paradigm to a friend of
mine
-rw-r--r-- | bless-demo | 37 | ||||
-rw-r--r-- | new-demo | 40 |
2 files changed, 77 insertions, 0 deletions
diff --git a/bless-demo b/bless-demo new file mode 100644 index 0000000..55f8734 --- /dev/null +++ b/bless-demo @@ -0,0 +1,37 @@ +#!/usr/bin/env perl + +# Class for the object +package Sanctum::Bless::Class; +use strict; +use warnings; +use utf8; +use 5.010; +our $VERSION = 0.1; +use Data::Printer; ## no critic (ProhibitDebuggingModules) + +sub spit { + my ($self) = @_; + p $self; + return; +} +1; + +# Demonstration of bless() +package Sanctum::Bless::Demo; ## no critic (ProhibitMultiplePackages) +use strict; +use warnings; +use utf8; +use 5.010; +our $VERSION = 0.1; ## no critic (ProhibitReusedNames) + +# Create a new hash reference +my $hash = { ayy => 'lmao', }; + +# Bless the hash reference into the Sanctum::Bless::Class. It thereby becomes +# an *instance* of "Sanctum::Bless:Class", which means it gains the "spit" +# method defined for it. +bless $hash, 'Sanctum::Bless::Class'; +bless $hash, 'Sanctum::Bless::Wizard'; + +# Run the spit() method that's now available on the hashref. +$hash->spit(); diff --git a/new-demo b/new-demo new file mode 100644 index 0000000..57e5e8b --- /dev/null +++ b/new-demo @@ -0,0 +1,40 @@ +#!/usr/bin/env perl + +# Class for the object +package RPG::Wizard; +use strict; +use warnings; +use utf8; +use 5.010; +use Carp; +our $VERSION = 0.1; + +sub new { + my ($class) = @_; + return bless {}, $class; +} + +sub zap { + my ($self) = @_; + say 'Zap!' or croak; + return; +} +1; + +# Demonstration of PACKAGE::new() +package Sanctum::New::Demo; ## no critic (ProhibitMultiplePackages) +use strict; +use warnings; +use utf8; +use 5.010; +use Data::Printer; ## no critic (ProhibitDebuggingModules) +our $VERSION = 0.1; ## no critic (ProhibitReusedNames) + +# Make a wizard object +my $wizard = RPG::Wizard->new(); + +# Print the object +p $wizard; + +# Zap! +$wizard->zap(); |