Open local file for output

Discussion in 'Perl Misc' started by Alan, Dec 21, 2006.

  1. Alan

    Alan Guest

    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
     
    Alan, Dec 21, 2006
    #1
    1. Advertisements

  2. 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
     
    Jürgen Exner, Dec 21, 2006
    #2
    1. Advertisements

  3. Ben Morrow

    Ben Morrow Guest

    Quoth "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.


    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. []
    'We come in peace'---the order came to cut them down.
     
    Ben Morrow, Dec 21, 2006
    #3
  4. Jürgen Exner <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Dec 21, 2006
    #4
  5. gf

    gf Guest

    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.
     
    gf, Dec 21, 2006
    #5
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Franck

    Open file on local machine

    Franck, Aug 15, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    7,019
    Eliyahu Goldin
    Aug 15, 2005
  2. Jeffrey Jones

    File Output to folder in local website

    Jeffrey Jones, Sep 12, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    384
    Jeffrey Jones
    Sep 12, 2005
  3. =?Utf-8?B?WVNVVA==?=

    Access local port or Running local exe file

    =?Utf-8?B?WVNVVA==?=, Jan 14, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    644
    =?Utf-8?B?WVNVVA==?=
    Jan 14, 2006
  4. Michael Peters

    ARGH! can't open a local file

    Michael Peters, Sep 13, 2004, in forum: HTML
    Replies:
    4
    Views:
    1,043
    Mark Parnell
    Sep 14, 2004
  5. lurker
    Replies:
    1
    Views:
    833
    Disco Octopus
    Apr 5, 2005
  6. Lyndon
    Replies:
    1
    Views:
    672
    Leonard Blaisdell
    Jul 25, 2005
  7. Michael
    Replies:
    1
    Views:
    1,902
    Ron Natalie
    Dec 1, 2004
  8. Schüle Daniel

    File::open and File.open

    Schüle Daniel, Oct 23, 2006, in forum: Ruby
    Replies:
    4
    Views:
    423
    Rick DeNatale
    Oct 24, 2006
Loading...