Perl getservent Function

Advertisements


Description

This function gets the next entry from the list of service entries, returning:

($name, $aliases, $port_number, $protocol_name)

This call iterate through /etc/services file.

Syntax

Following is the simple syntax for this function:

getservent

Return Value

This function returns undef on error otherwise service name in scalar context and empty list on error otherwise Service record (name, aliases, port number, protocol name) in list context.

Example

Following is the example code showing its basic usage:

#!/usr/bin/perl

while(($name, $aliases, $port_number, 
     $protocol_name) = getservent()){

   print "Name = $name\n";
   print "Aliases = $aliases\n";
   print "Port Number = $port_number\n";
   print "Protocol Name = $protocol_name\n";
}

When above code is executed, it produced following result

Name = tcpmux
Aliases = 
Port Number = 1
Protocol Name = tcp
Name = tcpmux
Aliases = 
Port Number = 1
Protocol Name = udp
Name = rje
Aliases = 
Port Number = 5
Protocol Name = tcp
Name = rje
Aliases = 
Port Number = 5
Protocol Name = udp
.
.
.
Name = iclpv-sc
Aliases = 
Port Number = 1390
Protocol Name = tcp
Name = iclpv-sc
Aliases = 
Port Number = 1390
Protocol Name = udp
Name = iclpv-sas
Aliases = 
Port Number = 1391
Protocol Name = tcp
Name = iclpv-sas
Aliases = 
Port Number = 1391
Protocol Name = udp
Na


Advertisements
Advertisements