tobold.org

correct • elegant • free

△ comp.lang.perl △

◅ DBM under Perl 5

doing efficient "ls" from within perl script ▻

File opening problem.

In article <4i61oa$1b57@ctsc.hkbc.hk>,
Mr. Chow Wing Siu <Johnson.Chow@Comp.HKBU.Edu.HK> wrote:
>But later, it dies out due to open problem. The file still exists
>but cannot open.  Why?

Don't ask the net, ask the system!  Whenever a system call in
Perl fails, you'll find that the variable $! (or $ERRNO, if you
said `use English') contains the reason for failure.

>   open (HOSTS, "/tmp/hosts.$$") || die "can't open";

I'd write the above like this.

    open HOSTS, "/tmp/hosts.$$" or
        die "$0: can't open /tmp/hosts.$$ for input: $!\n";

Now it shows you what program failed, exactly what it was trying
to do when it failed, and reason for failure.  (I've also assumed
Perl 5, so left out some unnecessary brackets and used `or' instead
of `||'.)

>                        &test($host);

You don't show us sub test; my guess is that it is leaking file
descriptors (i.e. opening a file and not closing it).  If this
guess is correct, $! will end up as something like "Too many open
files".

Tim.
--
Tim Goodwin   | "Those who will not study history are
Cambridge, UK | doomed to debug it." -- Barry Shein

Original headers:

From: tim@pipex.net (Tim Goodwin)
Newsgroups: comp.lang.perl.misc
Subject: Re: File opening problem.
Date: 14 Mar 1996 18:01:59 GMT
Organization: Unipalm PIPEX
Message-ID: <4i9mun$856@wave.news.pipex.net>
References: <4i61oa$1b57@ctsc.hkbc.hk>

△ comp.lang.perl △

◅ DBM under Perl 5

doing efficient "ls" from within perl script ▻