Tweet

How do I tell which operating system my Perl program is running in?

Solution: Use a special variable

There are many special, read-only Perl variables accessible from anywhere in any Perl script. One of them tells you the operating system the script is running in, as the following code demonstrates:

    #!/usr/bin/perl
    use strict;
    use warnings;

    my $os = $^O;
    print "Running in: $os\n";

    exit 0;

Under linux the output of this program would be:

    Running in: linux

Under windows it would be:

    Running in: MSWin32

See also

See perldoc perlvar for more information.

Revision: 1.5 [Top]