Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

flat file problem

by csuhockey3 (Curate)
on Oct 28, 2004 at 07:24 UTC ( [id://403292]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,
I have a very old form that I was asked to touch up and add some functionality to. I have everything all taken care of except writing the form data to a flat file. I have done this many many times in the past but I can't seem to figure out why this one isn't working. Here is a snippet:
... $FieldName = $cgi->param('Field Name'); ... sub flatFile{ open(FLATFILE, ">> /tmp/flatfile.data") or die $!; #will loop through and tab params, just for testing print FLATFILE $FieldName; close (FLATFILE); }
When I run the script from the command line (off-line mode), it creates/writes to flatfile.data. When I run from the form (on submit), it does not create or write to flatfile.data, however a confrimation email is sent and everything else is fine. Permissions are correct and I get no warnings or errors. Any ideas?

Thanks Monks!
update: code typo

Replies are listed 'Best First'.
Re: flat file problem
by gothic_mallard (Pilgrim) on Oct 28, 2004 at 07:36 UTC

    Your code syntax above is incorrect - the line:

    open(FLATFILE, ">> /tmp/flatfile.data" or die $!...);

    ..should read:

    open(FLATFILE, ">> /tmp/flatfile.data") or die $!;

    I don't know whether this is just an error you introduced while posting this question otherwise it should have given you an error.

    Apart from that I can't see anything wrong with the file writing logic which leads me to a couple of conclusions:

    Are you sure the permissions are correct? Chances are when you run it via the form it's not using same user account that it does at the command line - and that account may not have permission to write (which is likely as most "web-user" accounts on servers have very few permissions by default for security reasons).

    What platform/OS etc is this running on may I ask?

    --- Jay

    All code is untested unless otherwise stated.
    All opinions expressed are my own and are intended as guidance, not gospel; please treat what I say as such and as Abigail said Think for yourself.
    If in doubt ask.

      Hi gothic_mallard and pelagic . Sorry for the code typo -- indeed it was a mistake writing my post.

      I am working on a development server (Solaris). I will check agian tomorrow and verify my permissions are correct on both the development and production servers. I am wondering if documentroot might be the issue. I suppose it's possible to have /tmp in a different location on each of those servers, although I thought they are mirrors. Thanks for the quick replies from both of you.
Re: flat file problem
by PodMaster (Abbot) on Oct 28, 2004 at 08:07 UTC
    When I run from the form (on submit), it does not create or write to flatfile.data, however a confrimation email is sent and everything else is fine.
    Thats hard to believe :) You've made an assumption somewhere, and you're not checking something you should be. Its impossible to tell you what without seeing more. Maybe you're not pointing to the right cgi, maybe you're checking the wrong log, maybe flatFile doesn't get called, maybe you're in a chroot jail and /tmp/flatfile.data is really /tmp/apachejails/username/tmp/flatfile.data .... look into using Devel::Trace (or similar) or just start sprinkling around print/warn statements to verify that what you believe is going on, is actually going on.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      Was I naive in doing this?
      $working_dir = $0; $working_dir =~ s#(.*)/.*$#$1#; chdir $working_dir;
      Honestly, on submit, I get a confirmation page and an email, but no flat file (that's why I am pulling my hair out). I also made certain it was the correct cgi. I have never had a flat file give me fits like this! I might have to dig deeper into this new development server, I will run Devel::Trace right now.

      update:BINGO! It was a problem with the development server. I had the form on the production side and the cgi on the development side. After running Devel::Trace I saw where everything was going and that solved the problem (once the flatfile was created manually and placed on production side, it worked). Thank you all for the help.
        Yes, but not for reasons you may think :) The proper way to determine the directory where your file resides is using File::Spec->dirname( File::Spec->rel2abs(__FILE__)) (dirname not official part of File::Spec, get it from here). chdir would not affect sub flatFile because it uses an absolute path.

        MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
        I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
        ** The third rule of perl club is a statement of fact: pod is sexy.

Re: flat file problem
by pelagic (Priest) on Oct 28, 2004 at 07:34 UTC
    csuhockey3,
    did you verify the "DocumentRoot" setting of your webserver? Maybe the file is written to some other place below "DocumentRoot" ...

    pelagic

      Good point... depending on your system it can have funny ideas about where it "thinks" it's running as opposed to where it actually is.

      Certainly under ASP on IIS (for example), wherever your script is physically located unless you specifically set otherwise it has a habit of defaulting the current working directory to C:/windows/system32 (or equivalent).

      Can lead to a lot of frustrated head scratching before you work that one out :)

      --- Jay

      All code is untested unless otherwise stated.
      All opinions expressed are my own and are intended as guidance, not gospel; please treat what I say as such and as Abigail said Think for yourself.
      If in doubt ask.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://403292]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-25 09:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found