determine the operating system a script is running under
( categories: special variables )The name of the underlying operating system is stored in the variable $^O.
Common $^O values
| operating system | $^O value |
| Linux | linux |
| Windows | MSWin32 |
| AIX | aix |
| Solaris | solaris |
Note
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.
Example:
disable output buffering
( categories: special variables )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.
Example:
define constant values
( categories: special variables )The best way to define constant values is to use the Readonly module:
Example:
use Readonly;
Readonly my $PI => 3.1415926535;
NOTES:
- 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: system | special variables )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
NOTES:
- 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: special variables )All the environment variables can be accessed through the %ENV hash variable
Example:
print "Your current PATH environment variable is $ENV{PATH} \n";
NOTE:
You can add or modify environment variables using %ENV, but the changes are only valid for the current and child processes.
