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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Using Filehandles
by arturo (Vicar) on Apr 10, 2001 at 00:38 UTC

    To output text to a file, you simply do:

    open FILEHANDLE, "> $filename" or die "Can't write to $filename: $!\n" +; print FILEHANDLE "Whatever you're printing"; close FILEHANDLE;

    The Filehandle module allows you to treat filehandles like regular scalars, but it sounds like you don't need such functionality for what you want to do. And yes, you *must* specify a file to which you're going to write; but if you want to build all of your data up first, you should be saving it into an array or string. Read up on open and print for more info.

    HTH

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Re: Using Filehandles
by Asim (Hermit) on Apr 10, 2001 at 00:59 UTC

    I'm going to add the reply to your latest node here, Confused -- please add futher responses as a reply, NOT as a new node. Confused asked:

    I understand that the output file can be specified when using a filehandle. If you use Print to output a file, how do you specify where the output goes?

    There are a couple of ways. The quickest is like this (note that I'm using open, and not use Filehandle):

    my $SQLlog = 'SQL.log'; open (SQL_LOG, ">$SQLlog") or warn "Cannot open SQL commands logging a +t $SQLlog: $!\n"; print SQL_LOG "\n==========SQLCMDLOG===================BEGIN========== +====================\n";

    To break it down, first, define the name of the file the data is going into. This does not have to be done in a seperate varabile, but it's easier for debugging. Then use the open command to open the file for writing, and then print the data, but put the filehandle you defined before the data. In the code above, I say "print to the filehandle SQL_LOG such-and-such data."

    Again, please look at the docs for open and print, and try the code above.

    ----Asim, known to some as Woodrow.

Re: Using Filehandles
by Asim (Hermit) on Apr 10, 2001 at 00:42 UTC

    No, you _can_ just send the vars to STDOUT, which is how CGI/HTML data is sent normally.

    But are you writing this into a HTML file, or using it as a CGI app? If the former, you need to use the Filehandle module or a similar statement as you list, and you must specify a actual filename to send it to, as others have alreayd mentioned (the file does not have to exist, but it will by the time you write to it). If it's straight CGI to a browser, then you don't need any of the file stuff, just print and the rest'll be taken care of.

    Perhaps you should give us more detail in a comment below, so that we can properly help you.

    ----Asim, known to some as Woodrow.

Re: Using Filehandles
by astanley (Beadle) on Apr 10, 2001 at 00:47 UTC
    Confused - it looks to me like what you're trying to do is similar to the way an SSI script is parsed by the webserver. No you do not have to specify an output file - you can just do a regular print statement. When you run a perl script (or any CGI for that matter) the CGI's stdout is sent straight to the viewers browser. This allows you to do something like:
    #!/usr/bin/perl use CGI; print header(); print "This text will show up in the users browser\n";

    Hope that helps!

    -Adam Stanley
    Nethosters, Inc.
Re: Using Filehandles
by rchiav (Deacon) on Apr 10, 2001 at 00:43 UTC
    1) why did you post another question instead of replying to the one your already started?

    2) if you open a file handle, it has to be to *something*. Is your question on how to make it so you can change what file you're outputting to, that's different. It's like someone asking you how they save a Word document without giving it a name. And if your program doesn't know what it's writing to, how can you know what to view? Maybe if you clairify why you don't want to specify the name, we might be able to gleen some insight into what you're really trying to do. I think not specifying a name for the file is part of your solution to another problem.

    Rich