1Perl::Critic::Policy::BUasPneegrrsl:C::o:PnCrtrorihitibibucit:te:RdPeofPlPeirrcolyt:oD:OoBrcaPunrmgoestn:ot:(aP3tr)ioohnibitRefProtoOrProto(3)
2
3
4

NAME

6       Perl::Critic::Policy::Bangs::ProhibitRefProtoOrProto - Create a clone()
7       method if you need copies of objects.
8

AFFILIATION

10       This Policy is part of the Perl::Critic::Bangs distribution.
11

DESCRIPTION

13       Many times you'll see code for object constructors that's been cut-and-
14       pasted from somewhere else, and it looks like this:
15
16           sub new {
17               my $proto = shift;
18               my $class = ref($proto) || $proto;
19               my $self  = bless {}, $class;
20               ...
21           }
22
23       The $class is derived from the first parameter, whether it's the class
24       name, or an existing object.  This lets you do this:
25
26           my $fido = Dog->new();
27
28       which is very common, and the less likely
29
30           my $rover = $fido->new();
31
32       Now, why would you want to instantiate an object based on the type of
33       another object?  If you want to make $rover a clone of $fido, then Dog
34       should have a "clone()" method, instead of overloading the meaning of
35       "new()".
36
37       That's all the "ref($proto) || $proto" does for you.  If you don't need
38       that dubious functionality, then write your constructors like this:
39
40           sub new {
41               my $class = shift;
42               my $self = bless {}, $class;
43           }
44
45       See also Randal Schwartz's take on it at
46       <http://www.stonehenge.com/merlyn/UnixReview/col52.html>.
47

CONFIGURATION

49       This Policy is not configurable except for the standard options.
50

AUTHOR

52       Andrew Moore <amoore@mooresystems.com>
53

ACKNOWLEDGMENTS

55       Adapted from policies by Jeffrey Ryan Thalhammer <thaljef@cpan.org>,
56       and work done by Andrew Moore <amoore@mooresystems.com>.
57
59       Copyright (C) 2006-2013 Andy Lester
60
61       This library is free software; you can redistribute it and/or modify it
62       under the terms of the Artistic License 2.0.
63
64
65
66perl v5.34.0           Perl::Criti2c0:2:1P-o0l7i-c2y2::Bangs::ProhibitRefProtoOrProto(3)
Impressum