XP is just a number | |
PerlMonks |
Re: Writing portable codeby tobyink (Canon) |
on Mar 08, 2013 at 09:51 UTC ( [id://1022369]=note: print w/replies, xml ) | Need Help?? |
Module::Implementation provides an easy way to load the "best" of multiple implementations of the same function. Its documentation mostly revolves around the situation where you have an XS implementation and a pure Perl implementation, and wish to load the XS if posssible, but fall back to pure Perl. But it works just as well to switch between OS-specific implementations, or Perl-version-specific implementations. That said, Perl does have conditional compilation. It's just not very pretty...
Update: For longer pieces of code, heredocs look quite nice...
Update II: Also, bear in mind that constants used in conditionals are optimized away by the compiler, so:
... the conditional should be optimized away at compile time, so you don't get the overhead of a string comparison operation every time the function gets called.
package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
In Section
Seekers of Perl Wisdom
|
|