Chapter 16. System Information

Getting User and Machine Information

Perl provides several facilities for finding out information about the user and machine that you are running on. These functions are provided via Win32 extensions (see Appendix B, for more information).

To retrieve the name of the user executing the script, use the Win32::LoginName function:

use Win32;
$name = Win32::LoginName;

To retrieve the name of the machine executing the script, use the Win32::NodeName function:

use Win32;
$machine = Win32::NodeName;

The Win32::NetAdmin module provides extensive functionality for administering users and groups. Here’s an simple example of how you might use it to retrieve the current user’s home directory:

use Win32::NetAdmin;
$username = Win32::LoginName; # grab the name of the current user
Win32::NetAdmin::UserGetAttributes("",
    $username, $password, $passwordage,
    $privilege, $homedir, $comment,
    $flags, $scriptpath);
print "The homedir for $username is $homedir\n";

For more information on using Win32::NetAdmin, explore the win32mod documentation.

As you explore Perl scripts on the Net, you’ll no doubt find scripts that refer to any of a myriad of Perl functions that access UNIX password and group files. At the time of this writing, these functions are not implemented in Perl on Win32 platforms, but you can usually duplicate the functionality (if it’s applicable) using one of the Win32 extension modules.

Get Learning Perl on Win32 Systems now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.