Perl::C...OrProto(3)

πŸ“¦
version 1.14 Β· 2 versions
, in fedora 43

πŸ’… NAME πŸ’…

ok so bestie, welcome to Perl::Critic::Policy::Bangs::ProhibitRefProtoOrProto β€” which is a whole mouthful, i know, i know 🫢 β€” and what it does is basically yell at you (lovingly) to create a clone() method if you need copies of objects. that's the whole vibe. that's the assignment. periodt.


✨ AFFILIATION ✨

This Policy is part of the Perl::Critic::Bangs distribution. it's giving squad member, it's giving family. πŸ’–


🌸 DESCRIPTION 🌸

ok so picture this: you're out there reading code, and you see an object constructor that has clearly been cut-and-pasted from somewhere else. and it looks like this, every single time:

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

and like. bestie. the $class here is being derived from the first parameter β€” doesn't matter if that parameter is the class name, or an already-existing object. it just... takes whatever you hand it. which lets you do this:

my $fido = Dog->new();

super common, super normal, everyone does this, iconic behaviour, no notes. πŸ• and then it ALSO lets you do this, which is the less likely one:

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

and now the real question, the vibe check of it all: why would you even want to instantiate an object based on the type of another object?? πŸ€” if what you actually want is to make $rover a clone of $fido, then Dog should have its own clone() method, bestie. full stop. you should NOT be overloading the meaning of new(). that's not new()'s job. new() did not sign up for this. 😭

so yeah β€” that's literally all that "ref($proto) || $proto" does for you. that's the entire contribution. if you don't need that... let's be real, dubious functionality, then write your constructors like this instead:

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

clean. simple. main character energy. πŸ‘‘

also pls go read Randal Schwartz's take on it at <http://www.stonehenge.com/merlyn/UnixReview/col52.html> β€” it's linked right there, it's a whole thing, iykyk πŸ’œ

πŸŽ€ CONFIGURATION πŸŽ€

This Policy is not configurable except for the standard options. that's it. that's the whole section. no custom knobs for you, babe, sorry not sorry πŸ™ˆ


πŸ¦‹ 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>. legends, honestly, we stan 🫢


Copyright (C) 2006-2013 Andy Lester

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


and that's the whole page, bestie!! 🌈 this was perl v5.42.0, dated 2025-07-25, brought to you by Perl::C...OrProto(3) β€” go write a clone() method and slay the day πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from