Open local file for output

A

Alan

Hi all,
I am trying to send output to a local file and not having much luck
with it.

using this bit of code:
.. . .
open (LOCAL, ">c:\\important_stuff\\file_name.txt");
foreach (@data) {print LOCAL "$_";}
.. . .
results in a file named "c:\important_stuff\file_name.txt" in the
CGI-BIN directory of the server.

Any comments are appriciated,
-Alan
 
J

Jürgen Exner

Alan said:
I am trying to send output to a local file and not having much luck
with it.

using this bit of code:
. . .
open (LOCAL, ">c:\\important_stuff\\file_name.txt");

1: you are suffering from leaning toothpick syndrom.
'c:/important_stuff/filename.txt' works just as well, is much easier on the
the eyes, and much less likely to get wrong.
2: you forgot to check if the open was successful. Always, yes always check:

open (LOCAL, '>c:/important_stuff/file_name.txt')
or die "Cannot open c:/important_stuff/file_name.txt because of
$!\n";
foreach (@data) {print LOCAL "$_";}
. . .
results in a file named "c:\important_stuff\file_name.txt"

That's what the code is supposed to do.
in the CGI-BIN directory of the server.


???
You lost me.

jue
 
B

Ben Morrow

Quoth "Alan said:
Hi all,
I am trying to send output to a local file and not having much luck
with it.

using this bit of code:
. . .
open (LOCAL, ">c:\\important_stuff\\file_name.txt");
foreach (@data) {print LOCAL "$_";}
. . .
results in a file named "c:\important_stuff\file_name.txt" in the
CGI-BIN directory of the server.

A random stab in the dark: you 'server' is not running Windows, but
Unix. Unix does not consider \ to be a path separator, so this is
correct behaviour. If I am right, then the path is incorrect anyway:
Unix machines do not have the concept of 'drive'. Another (more) random
stab: this is in a CGI script, and you want to create a file on the
machine the browser is on when someone visits your page. If this is what
you are trying to do, it's impossible, for very good security reasons.
You need to understand the interaction between browser, web server and
CGI program a little better.

Ben
 
T

Tad McClellan

Jürgen Exner said:
Alan wrote:

1: you are suffering from leaning toothpick syndrom.


He is suffering, but not from LTS.

Leaning toothpick syndrome applies when the slashes are leaning
in _both_ directions, eg: /\/etc\/hosts/
 
G

gf

Alan said:
Hi all,
I am trying to send output to a local file and not having much luck
with it.

using this bit of code:
. . .
open (LOCAL, ">c:\\important_stuff\\file_name.txt");
foreach (@data) {print LOCAL "$_";}
. . .
results in a file named "c:\important_stuff\file_name.txt" in the
CGI-BIN directory of the server.

Here's some additional things to keep in mind beyond those already
mentioned:

1. Don't use "LOCAL" for a filehandle. It's too close to the spelling
of the reserved word ("local") in Perl. That's a debugging and
maintenance issue.
2. Use the three-parameter version of open(). There's lots of reasons,
but to me they boil down to avoiding debugging and maintenance issues
again.
3. Look into using the built-in File::Spec module for filename
portability across platforms. Macs running Classic OS, PCs (DOS and
Windows), Unix, and VMS systems all have different ways of pointing to
files and defining paths and drives. Using File::Spec allows you to do
it using Perl building blocks that will transparently handle the
differences for you.
4. Consider installing the CPAN "Perl::Critic" and "criticism" modules
and using the "use criticism" pragma during the development of your
app. They'll refer you to Conway's "Perl Best Practices" book which
will expand on why you want to do these sort of things.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top