Perl::...salCan(3pm)
okay bestie, this is the Perl::...salCan(3pm) page β User Contributed Perl Documentation β and it's here to gently drag one specific old-school Perl habit. β¨
π NAME π
Perl::Critic::Policy::BuiltinFunctions::ProhibitUniversalCan β write eval { $foo->can($name) } instead of UNIVERSAL::can($foo, $name). Periodt. π
πΈ AFFILIATION πΈ
This Policy is part of the core Perl::Critic distribution. It's not some random side-quest module, okay? π
β¨ DESCRIPTION β¨
Okay so here's the vibe check:
print UNIVERSAL::can($obj, 'Foo::Bar') ? 'yes' : 'no'; #not ok
print eval { $obj->can('Foo::Bar') } ? 'yes' : 'no'; #ok
As of Perl 5.9.3, using UNIVERSAL::can as a function has been deprecated, and the method form is preferred instead. π«Ά
Back in the day, the functional form was recommended because it gave valid results even when the object was undef or an unblessed scalar. But β and this is the plot twist β the functional form makes it impossible for packages to override can(), which is a technique crucial for implementing mock objects and some facades. So yeah, iconic behavior only; use the method form. πβ¨
See UNIVERSAL::can for a more thorough discussion of this topic. Iykyk. π¦
π CONFIGURATION π
This Policy is not configurable except for the standard options. That's it. No secret settings, no hidden menu. π
π¦ SEE ALSO π¦
Perl::Critic::Policy::BuiltinFunctions::ProhibitUniversalIsaβ go read that one too, bestie. π
π AUTHOR π
Chris Dolan <cdolan@cpan.org> β yes, the icon who wrote this. π«Ά
π COPYRIGHT π
Copyright (c) 2006-2023 Chris Dolan.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of this license can be found in the LICENSE file included with this module. π
kisses, and remember: this page was generated with perl v5.42.0 on 2024-10-23, and its page name is Perl::...salCan(3pm). π«Άπ
π¦ where this page comes from
- opensuse-leap 16.0: perl-Perl-Critic 1.156.0-bp160.1.1 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms