Skip site navigation (1)Skip section navigation (2)

FreeBSD Manual Pages

  
 
  

home | help
Perl::Critic::Policy::UserlContributedoPerl:DocumentatihibitRefProtoOrProto(3)

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

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

DESCRIPTION
       Many times you'll see code for object constructors that's been cut-and-
       pasted from somewhere else, and it looks	like this:

	   sub new {
	       my $proto = shift;
	       my $class = ref($proto) || $proto;
	       my $self	 = bless {}, $class;
	       ...
	   }

       The $class is derived from the first parameter, whether it's the	class
       name, or	an existing object.  This lets you do this:

	   my $fido = Dog->new();

       which is	very common, and the less likely

	   my $rover = $fido->new();

       Now, why	would you want to instantiate an object	based on the type of
       another object?	If you want to make $rover a clone of $fido, then Dog
       should have a "clone()" method, instead of overloading the meaning of
       "new()".

       That's all the "ref($proto) || $proto" does for you.  If	you don't need
       that dubious functionality, then	write your constructors	like this:

	   sub new {
	       my $class = shift;
	       my $self	= bless	{}, $class;
	   }

       See also	Randal Schwartz's take on it at
       <http://www.stonehenge.com/merlyn/UnixReview/col52.html>.

CONFIGURATION
       This Policy is not configurable except for the standard options.

AUTHOR
       Andrew Moore <amoore@mooresystems.com>

ACKNOWLEDGMENTS
       Adapted from policies by	Jeffrey	Ryan Thalhammer	<thaljef@cpan.org>,
       and work	done by	Andrew Moore <amoore@mooresystems.com>.

COPYRIGHT
       Copyright (C) 2006-2011 Andy Lester

       This library is free software; you can redistribute it and/or modify it
       under the terms of the Artistic License 2.0.

perl v5.24.1	       Perl::Critic::Policy::Bangs::ProhibitRefProtoOrProto(3)

NAME | AFFILIATION | DESCRIPTION | CONFIGURATION | AUTHOR | ACKNOWLEDGMENTS | COPYRIGHT

Want to link to this manual page? Use this URL:
<https://www.freebsd.org/cgi/man.cgi?query=Perl::Critic::Policy::Bangs::ProhibitRefProtoOrProto&sektion=3&manpath=FreeBSD+12.0-RELEASE+and+Ports>

home | help