ZenOfCoding has asked for the wisdom of the Perl Monks concerning the following question:

Hey all... Every time I try to call
read ($file, $data, 1024);
my program gives up. Does absolutely nothing and processes no othe commands on the page. Any thoughts?

Replies are listed 'Best First'.
Re: read(x,y,z)
by tadman (Prior) on Jun 28, 2002 at 21:49 UTC
    First, check that you're using strict and -w. I think you're getting errors that you can't see. In that case, check your log file.

    Second, the way this operates depends a lot on how you're calling it. If you're opening a pipe, it could break, and then things could fall apart.

    I have a sneaking suspicion, though, that you're doing something like this:
    my $file = "foo.txt"; my $data; read($file, $data, 1024);
    Note that read operates on a file-handle, not a file name. These need to be opened:
    my $file = "foo.txt"; open ($fh, $file) || die "Could not open file $file\n"; my $data; read($fh, $data, 1024);
    If you're feeling more progressive, you might try and use IO::Handle instead of the read anachronism.
Re: read(x,y,z)
by ides (Deacon) on Jun 28, 2002 at 21:49 UTC
    Make sure $file is a file handle and not just a scalar with the path to the file you are wanting to work with.

    -----------------------------------
    Frank Wiles <frank@wiles.org>
    http://frank.wiles.org

Context and intent, please?
by cebrown (Pilgrim) on Jun 28, 2002 at 21:53 UTC
    Could you provide some more context (sample code)? What exactly are you trying to do with the 1k chunk that you're trying to read?

    Among the things you should check in the meantime are:

    • is the $file a valid filehandle?
    • do you have permissions to the file? (you say "page" so I assume you are running on a webserver -- does the webserver have permissions to the file?)
Re: read(x,y,z)
by Aristotle (Chancellor) on Jun 29, 2002 at 11:48 UTC

    On the page? Do you mean you're running a CGI script? In that case, use CGI::Carp 'fatalsToBrowser'; will help you see any errors that will otherwise onlyl show up in the server's logs. Be sure however to drop the 'fatalsToBrowser' option once you're script is actually running on the production server.

    For a good intro to the topic of CGI scripts, see Ovid's excellent CGI course.

    Makeshifts last the longest.