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.

[Top]