home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2000 May
/
Chip_2000-05_cd1.bin
/
zkuste
/
Perl
/
ActivePerl-5.6.0.613.msi
/
䆊䌷䈹䈙䏵-䞅䞆䞀㡆䞃䄦䠥
/
_458f44e7674240ab351bc223afedac75
< prev
next >
Wrap
Text File
|
2000-03-23
|
6KB
|
153 lines
<HTML>
<HEAD>
<TITLE>Net::netent - by-name interface to Perl's built-in getnet* functions</TITLE>
<LINK REL="stylesheet" HREF="../../Active.css" TYPE="text/css">
<LINK REV="made" HREF="mailto:">
</HEAD>
<BODY>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
<TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
<STRONG><P CLASS=block> Net::netent - by-name interface to Perl's built-in getnet* functions</P></STRONG>
</TD></TR>
</TABLE>
<A NAME="__index__"></A>
<!-- INDEX BEGIN -->
<UL>
<LI><A HREF="#name">NAME</A></LI><LI><A HREF="#supportedplatforms">SUPPORTED PLATFORMS</A></LI>
<LI><A HREF="#synopsis">SYNOPSIS</A></LI>
<LI><A HREF="#description">DESCRIPTION</A></LI>
<LI><A HREF="#examples">EXAMPLES</A></LI>
<LI><A HREF="#note">NOTE</A></LI>
<LI><A HREF="#author">AUTHOR</A></LI>
</UL>
<!-- INDEX END -->
<HR>
<P>
<H1><A NAME="name">NAME</A></H1>
<P>Net::netent - by-name interface to Perl's built-in getnet*() functions</P>
<P>
<HR>
<H1><A NAME="supportedplatforms">SUPPORTED PLATFORMS</A></H1>
<UL>
<LI>Linux</LI>
<LI>Solaris</LI>
<LI>Windows</LI>
</UL>
<HR>
<H1><A NAME="synopsis">SYNOPSIS</A></H1>
<PRE>
use Net::netent qw(:FIELDS);
getnetbyname("loopback") or die "bad net";
printf "%s is %08X\n", $n_name, $n_net;</PRE>
<PRE>
use Net::netent;</PRE>
<PRE>
$n = getnetbyname("loopback") or die "bad net";
{ # there's gotta be a better way, eh?
@bytes = unpack("C4", pack("N", $n->net));
shift @bytes while @bytes && $bytes[0] == 0;
}
printf "%s is %08X [%d.%d.%d.%d]\n", $n->name, $n->net, @bytes;</PRE>
<P>
<HR>
<H1><A NAME="description">DESCRIPTION</A></H1>
<P>This module's default exports override the core <A HREF="../../lib/Pod/perlfunc.html#item_getnetbyname"><CODE>getnetbyname()</CODE></A> and
<A HREF="../../lib/Pod/perlfunc.html#item_getnetbyaddr"><CODE>getnetbyaddr()</CODE></A> functions, replacing them with versions that return
``Net::netent'' objects. This object has methods that return the similarly
named structure field name from the C's netent structure from <EM>netdb.h</EM>;
namely name, aliases, addrtype, and net. The aliases
method returns an array reference, the rest scalars.</P>
<P>You may also import all the structure fields directly into your namespace
as regular variables using the :FIELDS import tag. (Note that this still
overrides your core functions.) Access these fields as variables named
with a preceding <CODE>n_</CODE>. Thus, <CODE>$net_obj->name()</CODE> corresponds to
$n_name if you import the fields. Array references are available as
regular array variables, so for example <CODE>@{ $net_obj->aliases()
}</CODE> would be simply @n_aliases.</P>
<P>The <CODE>getnet()</CODE> function is a simple front-end that forwards a numeric
argument to getnetbyaddr(), and the rest
to getnetbyname().</P>
<P>To access this functionality without the core overrides,
pass the <A HREF="../../lib/Pod/perlfunc.html#item_use"><CODE>use</CODE></A> an empty import list, and then access
function functions with their full qualified names.
On the other hand, the built-ins are still available
via the <CODE>CORE::</CODE> pseudo-package.</P>
<P>
<HR>
<H1><A NAME="examples">EXAMPLES</A></H1>
<P>The <CODE>getnet()</CODE> functions do this in the Perl core:</P>
<PRE>
sv_setiv(sv, (I32)nent->n_net);</PRE>
<P>The <CODE>gethost()</CODE> functions do this in the Perl core:</P>
<PRE>
sv_setpvn(sv, hent->h_addr, len);</PRE>
<P>That means that the address comes back in binary for the
host functions, and as a regular perl integer for the net ones.
This seems a bug, but here's how to deal with it:</P>
<PRE>
use strict;
use Socket;
use Net::netent;</PRE>
<PRE>
@ARGV = ('loopback') unless @ARGV;</PRE>
<PRE>
my($n, $net);</PRE>
<PRE>
for $net ( @ARGV ) {</PRE>
<PRE>
unless ($n = getnetbyname($net)) {
warn "$0: no such net: $net\n";
next;
}</PRE>
<PRE>
printf "\n%s is %s%s\n",
$net,
lc($n->name) eq lc($net) ? "" : "*really* ",
$n->name;</PRE>
<PRE>
print "\taliases are ", join(", ", @{$n->aliases}), "\n"
if @{$n->aliases};</PRE>
<PRE>
# this is stupid; first, why is this not in binary?
# second, why am i going through these convolutions
# to make it looks right
{
my @a = unpack("C4", pack("N", $n->net));
shift @a while @a && $a[0] == 0;
printf "\taddr is %s [%d.%d.%d.%d]\n", $n->net, @a;
}</PRE>
<PRE>
if ($n = getnetbyaddr($n->net)) {
if (lc($n->name) ne lc($net)) {
printf "\tThat addr reverses to net %s!\n", $n->name;
$net = $n->name;
redo;
}
}
}</PRE>
<P>
<HR>
<H1><A NAME="note">NOTE</A></H1>
<P>While this class is currently implemented using the Class::Struct
module to build a struct-like class, you shouldn't rely upon this.</P>
<P>
<HR>
<H1><A NAME="author">AUTHOR</A></H1>
<P>Tom Christiansen</P>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
<TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
<STRONG><P CLASS=block> Net::netent - by-name interface to Perl's built-in getnet* functions</P></STRONG>
</TD></TR>
</TABLE>
</BODY>
</HTML>