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

Hi I am trying to build a web form where it can take the input from the user and write it to a file. And when I will open that form again that for should read the file that was created at the 1st step and all the fields should auto populate from that file. I have 20 text fields in my form. I have completed the 1st part where I can write the form data to a file. Can you please help me how the form can read the file that created and auto populate the fields? Thanks Saurav

Replies are listed 'Best First'.
Re: Read/write file in CGI Perl
by mr_mischief (Monsignor) on May 06, 2014 at 14:42 UTC

    It will be very difficult to complete the code without seeing it in its current form. However, I can offer hints.

    If you read the HTML or XHTML documentation it will make clear that a form field can have an initial value set. If you are using a JavaScript library like jQuery or YUI your JavaScript library has a way to set these through the DOM, or you can do it with JavaScript directly. You can also have your Perl code dynamically write the HTML with the values already set. Some modules, such as CGI have ways to do that, as do the frameworks and toolkits for web work.

    As for opening and reading a file, there's open and then many ways to read. The readline ( '<>' ) is a good way to read a file. One might also use a structured file via XML like with the XML::Simple or XML::Twig modules; via CSV; via JSON, YAML, or INI, or using dbm.

    One could also use a database via the DBI or a similar module (DBI). You could then use a database backend like SQLite, DBD::mysql, DBD::Pg. or many others (DBD). There are database-like interfaces between the DBI and files like DBD::CSV or DBD::RAM if you want, but a real database may be better in the long run.

Re: Read/write file in CGI Perl
by Anonymous Monk on May 06, 2014 at 14:42 UTC

    I recommend you take a look at CGI::Session. There's also CGI::Application, which can do even more, although it may be too much if your script is simple.

    There are also some more modern frameworks such as Catalyst, Dancer and Mojolicious (I find Mojolicious::Lite especially nice for small web apps), although using those may require a re-structuring of existing code to meet those frameworks.

Re: Read/write file in CGI Perl
by InfiniteSilence (Curate) on May 06, 2014 at 18:05 UTC

    Why are people nowadays on Perlmonks being so accommodating to individuals that are posting requirements without any code?

    Perlmonks is here to help you write code and fix problems in that code; not to do your work for you. In the future please post your actual code or ask for online links to resources where you can learn more about the Perl language and/or about programming in general.

    Celebrate Intellectual Diversity

      I don't think this thread is too "accommodating" - the replies so far are pretty much "here's some documentation to read", which I think is appropriate for the level of effort the OP showed, and "online links to resources" is what you suggest anyway, right? I wouldn't require someone to post code to get some initial help.

      Personally, if the OP doesn't show code, I don't usually post code. If the OP posts a short question, I usually post a short answer. If the OP doesn't show any effort at all, I often don't reply at all.

      Why are people nowadays on Perlmonks being so accommodating to individuals that are posting requirements without any code?

      For the same reason they're accommodating to people posting in the wrong section?