Anti-nuisance lawsuit warning: The purpose of these notes is to remind me, Zoegond, of stuff or to help me work stuff out. They may contain mistakes.

Quick

  • ($a, $b....) = unpack("A2A7...", $packed)
  • push( array, list )

Friday, March 8, 2013

Redefining Perl built-ins

Suppose you're writing one of those 'extender' sort of packages that exports subs with nice friendly names. And you want to call one of the subs 'connect', or any other name that's also the name of a Perl built-in function.
  • You can't do seamlessly if the built-in function has complex arguments - eg print. You can test this by checking prototype("CORE::functionname") - if you get back a Perl prototype (eg "$$"), you can redefine the function as an exact replacement.
  • Put use subs "functionname" in your package
  • Define functionname in your package as you normally would
  • If you want the function to be overridden in ALL packages, even ones that haven't imported your extender package, export the function with *{"CORE::global::functionname"} = \&functionname as well as with *{"main::functionname"}. If you only want it to be overridden in importing packages, which is what I want if I'm just trying to give my 'extender' functions friendly names, export it with *{"main::functionname"} as usual.
  • It is sensible to point out in documentation that functionname replaces the built-in (so caller can say CORE::functionname if they wish to use the original)

http://perldoc.perl.org/perlsub.html#Overriding-Built-in-Functions

Followers

Blog Archive