File: instance.pl

package info (click to toggle)
libconstant-defer-perl 5-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 264 kB
  • ctags: 76
  • sloc: perl: 1,590; makefile: 2
file content (67 lines) | stat: -rwxr-xr-x 2,165 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/perl -w

# Copyright 2009, 2010, 2011 Kevin Ryde

# This file is part of constant-defer.
#
# constant-defer is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 3, or (at your option) any
# later version.
#
# constant-defer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with constant-defer.  If not, see <http://www.gnu.org/licenses/>.


# Usage: ./instance.pl
#
# This is roughly how to press constant::defer into service for a once-only
# object instance creation.
#
# The creation code here is entirely within the instance() routine so after
# it's run it's discarded, which may save a few bytes of memory.  If you
# wanted other object instances as well as a shared one then you'd split out
# a usual sort of new().
#
# The $class parameter can help subclassing.  A call like
#
#     MyClass::SubClass->instance
#
# blesses into that subclass.  But effectively there's only one instance
# behind the two MyClass and MyClass::SubClass and whichever runs first is
# the class created.  If you only ever want one of the two then that can be
# fine, otherwise it might be very bad.
#
# There's no reason not to take other arguments in the instance() creation,
# except that they only have an effect on the first call, so it may be more
# confusing than flexible.
#
# Generally you're best off using Class::Singleton (or
# Class::Singleton::Weak) but if you've got constant::defer for other things
# then this is compact and cute.
#

package MyClass;
use strict;

use constant::defer instance => sub {
  my ($class) = @_;
  return bless { foo => 123 }, $class;
};
sub do_something {
  print "do something ...\n";
}

package main;
printf "instance %s\n", MyClass->instance;
printf "instance %s\n", MyClass->instance;

my $obj = MyClass->instance;
$obj->do_something;

exit 0;