Cross-platform compatibility
Note: I wrote this for my project Ara's README but it's useful for other projects too so I made a seperate page for this.
Previously ara
had OpenBSD specific code & would simply fail to run on
other OSes, now it runs on all platforms. There is still OpenBSD
specific code but it's used only when ara
detects to be running on
OpenBSD.
use constant is_OpenBSD => $^O eq "openbsd"; require OpenBSD::Unveil if is_OpenBSD; sub unveil { if (is_OpenBSD) { return OpenBSD::Unveil::unveil(@_); } else { return 1; } }
is_OpenBSD
is a constant so the if-else block is optimized at compile
time. Another way would be to define the sub inside the if-else block
which is what I did initially but that is not the same thing as this.
You cannot define sub like that in Perl because this step happens at compile time & so the if-else block is ignored, which means the code will be equivalent to else block being true all the time because that's what comes later.
if (is_OpenBSD) { require OpenBSD::Unveil; OpenBSD::Unveil->import; } else { sub unveil { return 1; } }
Above code block will override the unveil sub to be return 1;
everytime,
this was fixed in commit 245aebe3da915afc0feafc7257f025e2e66a987f
.
This will still fail on OpenBSD if users don't have OpenBSD::Unveil
in
@INC
, this shouldn't be an issue with Perl in base but if user runs
custom Perl then it might not be in @INC
, in that case user is expected
to fix this by adding the path to OpenBSD::
in @INC
.