develooper Front page | perl.perl5.porters | Postings from February 2000

deprecating $?

From:
Tom Christiansen
Date:
February 17, 2000 15:22
Subject:
deprecating $?
Message ID:
6260.950829737@chthon
Either $? needs to be deprecated when used with gethost*(), or it
needs to become $!-ishly magical.  As it is, it's pretty mystertious,
because you can't get the real error string out of it.

------- Forwarded Message

Date:          Mon, 14 Feb 2000 05:37:24 MST
From:          Tom Christiansen <tchrist@chthon.perl.com>
Subject:       h_error
To:            perl5-porters@perl.org

It really does no good that gethost*() sets $? to h_error,
since it's not magical like $! and we don't have our own
hstrerror() function to use.

Please contrast:

    % perl gethost bogus stuff..here localhost
    gethost: host not found `bogus': 1
    gethost: host not found `stuff..here': 3
    localhost is 127.0.0.1

with 

    % a.out bogus stuff..here localhost
    a.out: host not found `bogus': Unknown host
    a.out: host not found `stuff..here': Unknown server error
    localhost is 127.0.0.1

- --tom

# This is a shell archive.  Save it in a file, remove anything before
# this line, and then unpack it by entering "sh file".  Note, it may
# create directories; files and directories will be owned by you and
# have default permissions.
#
# This archive contains:
#
#	gethost
#	gh.c
#
echo x - gethost
sed 's/^X//' >gethost << 'END-of-gethost'
Xuse Socket;
Xuse Net::hostent;
X
Xfor (@ARGV) { 
X    if ($h=gethostbyname($_)) {
X	printf "%s is %s\n", 
X	    $h->name, inet_ntoa($h->addr);
X    } 
X    else {
X	printf STDERR "%s: host not found `%s': %s\n", $0, $_, $?;
X	$oops++;
X    } 
X}
Xexit($oops != 0);
END-of-gethost
echo x - gh.c
sed 's/^X//' >gh.c << 'END-of-gh.c'
X#include <stdio.h>
X#include <netdb.h>
X#include <sys/socket.h>
X#include <netinet/in.h>
X#include <arpa/inet.h>
X
Xmain(int ac, char **av)
X{
X    struct hostent *hp;
X    char *progname = *av;
X    int errcount = 0;
X
X    while (--ac && ++av) {
X	if (!(hp = gethostbyname(*av))) {
X	    fprintf(stderr, "%s: host not found `%s': %s\n",
X		progname, *av, hstrerror(h_errno));
X	    errcount++;
X	    continue;
X	} 
X
X	printf("%s is %s\n", hp->h_name,
X	    inet_ntoa((*(struct in_addr *)hp->h_addr_list[0])));
X
X	continue;
X	/* NOTREACHED */
X
X	printf("official name: %s\n", hp->h_name);
X	if (hp->h_aliases) {
X	    char **cp;
X	    for (cp = hp->h_aliases; *cp; cp++) {
X		printf("Name alias: %s\n", *cp);
X	    } 
X	} 
X
X	if (hp->h_addr_list[1]) {
X	    char *cp;
X	    for (cp = hp->h_addr_list[1]; *cp; cp++) {
X		printf("Alt address: %s\n", 
X		    inet_ntoa((*(struct in_addr *)hp->h_addr_list[0])));
X	    }
X	} 
X
X    } 
X
X    exit(errcount != 0);
X} 
END-of-gh.c
exit



------- End of Forwarded Message




nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About