1. Here’s one way to do it.

    foreach $host (@ARGV) {
      ($name, $aliases, $addrtype, $length, @addrs) = gethostbyname($host);
      print "$host:\n";
    
      foreach $a (@addrs) {
        print join(".", unpack("C4", $a)), "\n";
      }
    }

    This code just takes a list of machine names, iterates over them, calling get-hostbyname() for each one. We then enumerate each of the addresses, printing them out in dotted decimal notation.

  2. Here’s one way to do it:

    use 
                      Win32::Registry;
    $p = shift || die "usage: $0 path";
    # strip leading backslashes
    $p =~ s#^\\##;
    $main::HKEY_LOCAL_MACHINE->Open($p, $key) || 
            die "Open: $!";
    $key->GetValues(\%vals); # get values -hash ref
    foreach $k (keys %vals) {
        $key = $vals{$k};
        print "$$key[0] = $$key[2]\n";
    }

    This code takes a path relative to HKEY_LOCAL_MACHINE (something like SOFTWARE\ActiveWare\Perl5) and strips beginning backslashes, if there are any. It opens the key using the precreated HKEY_LOCAL_MACHINE key. It then calls GetValues (passing it a reference to a hash; see Chapter 18, for more on references). The code then enumerates over the keys of the hash, printing them. Each value consists of a reference to a list with three items, so we assign the list reference to $key. We then have to dereference $key in order to access its values; we do so with the $$key[0] construct.

  3. Here’s one way to do it:

    sub CreateKeyPath { my ($subtree, $path) = @_; # break it into components # strip initial path separator, if there is one $path =~ s#^\\##; my (@klist) = split(/\\/, $path); my $key; ...

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.