Discussion:
Determining OS type and perl version
(too old to reply)
Sisyphus
2006-11-26 00:26:25 UTC
Permalink
Hi,
Where do I find the definitions (assuming such exist) that I can use to
determine from within an XSub:

1) The operating system in use (specifically, whether it's Cygwin);
2) The version of perl (specifically, whether it's >= 5.6.1);

In the past I've used the values of $] and $^O to DEFINE my own symbols in
the Makefile.PL - which works fine, but is a little silly if the info the
XSub needs is already available.

Cheers,
Rob
Muppet
2006-11-26 02:05:18 UTC
Permalink
Post by Sisyphus
Hi,
Where do I find the definitions (assuming such exist) that I can use to
1) The operating system in use (specifically, whether it's Cygwin);
2) The version of perl (specifically, whether it's >= 5.6.1);
In the past I've used the values of $] and $^O to DEFINE my own symbols in
the Makefile.PL - which works fine, but is a little silly if the info the
XSub needs is already available.
You can use those variables in XS the same as in perl code. This
works for me:

void
do_it ()
CODE:
/* Note that ^O is literally ^O, as in ^v^O in vim, or hex 0x0f.
* The control char doesn't want to show in my mailer so here
* it is in C hex. */
SV * os = get_sv ("\x0f", FALSE);
SV * version = get_sv ("]", FALSE);
if (os)
warn ("$^O = '%s'\n", SvPV_nolen (os));
if (version)
warn ("$] = '%s'\n", SvPV_nolen (version));

That prints

$^O = 'linux'
$] = '5.008003'

on my system.

But the perlvar manpage warns against using $^O on windows...

$^O The name of the operating system under which this
copy of Perl was
built, as determined during the configuration
process. The value
is identical to $Config{'osname'}. See also Config
and the -V
command-line switch documented in perlrun.

In Windows platforms, $^O is not very helpful: since
it is always
"MSWin32", it doesn't tell the difference between
95/98/ME/NT/2000/XP/CE/.NET. Use Win32::GetOSName() or
Win32::GetOSVersion() (see Win32 and perlport) to
distinguish
between the variants.

Dunno if that's relevant for using Cygwin.
Sisyphus
2006-11-26 03:23:00 UTC
Permalink
----- Original Message -----
From: "muppet" <***@asofyet.org>
To: "Sisyphus" <***@optusnet.com.au>
Cc: "xs" <perl-***@perl.org>
Sent: Sunday, November 26, 2006 1:05 PM
Subject: Re: Determining OS type and perl version
Post by Muppet
Post by Sisyphus
Hi,
Where do I find the definitions (assuming such exist) that I can use to
1) The operating system in use (specifically, whether it's Cygwin);
2) The version of perl (specifically, whether it's >= 5.6.1);
In the past I've used the values of $] and $^O to DEFINE my own symbols in
the Makefile.PL - which works fine, but is a little silly if the info the
XSub needs is already available.
You can use those variables in XS the same as in perl code.
Sorry - I didn't specify it, but I was wanting to do it with the
pre-processor:

SV * foo(Sv * arg) {
#ifdef USING_CYGWIN
// do stuff
#else
// do other stuff
#endif
}

SV * bar(Sv * arg) {
#ifdef PERL561_OR_LATER
// do it one way
#else
// do it another way
#endif
}

.
.
.
Post by Muppet
But the perlvar manpage warns against using $^O on windows...
The main concern there seems to be that $^O doesn't distinguish between the
various Windows versions (95, 98, XP, etc.).
That's rarely an issue - and Win32.pm has a function to deal with that issue
when it *does* arise.
Post by Muppet
Dunno if that's relevant for using Cygwin.
I think $^O would be suitable for my purposes wrt Cygwin - but, rather than
do it with a callback, I'd probably just put something like the following in
the WriteMakefile() section of the Makefile.PL:

DEFINE => $^O =~ /cygwin/i ? '-DUSING_CYGWIN' : '',

And I'm quite happy to do it that way - it's just I expected there would be
an #ifdef already in place that I ought to instead be using.
Not so ?

Cheers,
Rob

Loading...