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 (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