| 1 | # PiFlash::Object - object functions for PiFlash classes | ||||||||||
| 2 | # by Ian Kluft | ||||||||||
| 3 | |||||||||||
| 4 | 5 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 5 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 5 | ▶ | use v5.14.0; # require 2011 or newer version of Perl | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | package PiFlash::Object; | ||||||||||
| 9 | 5 | $PiFlash::Object::VERSION = '0.4.3'; | |||||||||
| 10 | 5 | ▶ | use autodie; # report errors instead of silently continuing ("die" actions are used as exceptions - caught & reported) | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 5 | ▶ | use Carp qw(confess); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | # ABSTRACT:object functions for PiFlash classes | ||||||||||
| 14 | |||||||||||
| 15 | |||||||||||
| 16 | # new() - internal function to instantiate hook object | ||||||||||
| 17 | # this should only be called from add() with coderef/caller/origin parameters | ||||||||||
| 18 | sub new | ||||||||||
| 19 | 0 | ▶ | { | ||||||||
|
Subroutine
new: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||
| 20 | 0 | my $class = shift; | |||||||||
| 21 | 0 | my $params = shift; | |||||||||
| 22 | |||||||||||
| 23 | # instantiate an object of the class | ||||||||||
| 24 | 0 | my $self = {}; | |||||||||
| 25 | 0 | bless $self, $class; | |||||||||
| 26 | |||||||||||
| 27 | # initialize parameters | ||||||||||
| 28 | 0 | foreach my $key (keys %$params) { | |||||||||
| 29 | 0 | $self->{$key} = $params->{$key}; | |||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | # chack for missing required parameters | ||||||||||
| 33 | 0 | my @missing; | |||||||||
| 34 | 0 | foreach my $required ($class->object_params()) { | |||||||||
| 35 | 0 | ▶ | exists $self->{$required} or push @missing, $required; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | } | ||||||||||
| 37 | 0 | ▶ | if (@missing) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | 0 | confess $class."->new() missing required parameters: ".join(" ", @missing); | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | # if init() class method exists, call it with any remaining parameters | ||||||||||
| 42 | 0 | ▶ | if ($class->can("init")) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 0 | $self->init(@_); | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | 0 | return $self; | |||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | 5 | 1; | |||||||||
| 50 | |||||||||||
| 51 | __END__ | ||||||||||