Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Open local file for output

Reply
Thread Tools

Open local file for output

 
 
Alan
Guest
Posts: n/a
 
      12-21-2006
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

 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      12-21-2006
Alan wrote:
> 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


 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      12-21-2006

Quoth "Alan" <(E-Mail Removed)>:
> 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

--
You poor take courage, you rich take care:
The Earth was made a common treasury for everyone to share
All things in common, all people one. [(E-Mail Removed)]
'We come in peace'---the order came to cut them down.
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      12-21-2006
Jürgen Exner <(E-Mail Removed)> wrote:
> Alan wrote:


>> open (LOCAL, ">c:\\important_stuff\\file_name.txt");

>
> 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/


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
gf
Guest
Posts: n/a
 
      12-21-2006

Alan wrote:
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: how to open a file in some application using Tkinter i am usingTKINTER to create GUI application i want to know how to open a worddocument in open office or any other applicatio Fredrik Lundh Python 1 01-09-2008 10:40 AM
Access local port or Running local exe file =?Utf-8?B?WVNVVA==?= ASP .Net 0 01-14-2006 12:41 AM
Playing a local mpeg file from a local HTML file... Lyndon HTML 1 07-25-2005 02:21 AM
Browser link to local file works when local, not work when servedfrom http lurker HTML 1 04-05-2005 05:10 AM
VC++ _popen to call ftp and pipe output to a file, cannot open file? Michael C++ 1 12-01-2004 09:50 PM



Advertisments