Skip navigation.
Your source for Perl tips, howto's, faq and tutorials

determine the operating system a script is running under

( categories: )

The name of the underlying operating system is stored in the variable $^O.

Common $^O values

 operating system  $^O value 


The value stored in $^O contains only the name of the operating system, not the release number. To determine the release number, consider using POSIX::uname() from POSIX package.


disable output buffering

( categories: )

Writing to STDOUT (or any other output filehandle) is buffered by default. To ask Perl to flush immediately after each write or print command, set the special variable $| to 1.

Setting this value is very helpful when you are printing to a web browser in a CGI script or writing to a socket.

Please notice that this setting does not apply to all output filehandles you may have, it only applies to the current selected output filehandle.

If you want to disable output buffering to a non selected handle, you must select it first.


define constant values

( categories: )

The best way to define constant values is to use the Readonly module:

use Readonly;
Readonly my $PI => 3.1415926535;

- You can define Readonly scalars, hashes and arrays.
- If you try to modify a Readonly variable, the program will die.

determine the user and/or group running a script

( categories: | )

Use the following predefined variables to get the user and group information belonging to the current process:

  • $< - real user id (uid); unique value
  • $> - effective user id (euid); unique value
  • $( - real group id (gid); list (separated by spaces) of groups
  • $) - effective group id (egid); list (separated by spaces) of groups

- This information applies only to Unix systems
- The values that theses variables hold are integers.

To get the user and group names, use '(getpwuid($<))[0]' (for user information) and 'getgrgid($()' (for groups).

reading and writing environment variables

( categories: )

All the environment variables can be accessed through the %ENV hash variable

print "Your current PATH environment variable is $ENV{PATH} \n";

You can add or modify environment variables using %ENV, but the changes are only valid for the current and child processes.

Syndicate content