Do you mean something like this?

#!/usr/bin/perl use Mojolicious::Lite -signatures; use File::Replace qw/replace3/; use Mojo::JSON qw/encode_json decode_json/; use Mojo::File; my $DATA_FILE = Mojo::File->new('/path/to/data.json'); helper defaults => sub ($c, $param) { return undef unless -e $DATA_FILE; my $defaults = decode_json( $DATA_FILE->slurp ); return $defaults->{$param}; }; get '/' => sub ($c) { $c->render(template => 'index') } => 'index'; post '/' => sub ($c) { my (undef,$outfh,$repl) = replace3($DATA_FILE); print $outfh encode_json { name => $c->param('name'), }; $repl->finish; $c->render(template => 'index', message => "Form submitted!"); } => 'submit'; app->start; __DATA__ @@ index.html.ep <!DOCTYPE html> <html> <head><title>Hello, World!</title></head> <body> % if ( stash 'message' ) { <p><%= stash 'message' %></p> % } %= form_for submit => ( method=>'post' ) => begin <div> %= label_for name => 'Your Name' %= text_field name => defaults 'name' </div> <div> %= submit_button 'Submit' </div> %= end <p> %= link_to Reset => 'index' </p> </body> </html>

I'm using my module File::Replace to prevent* problems when there are multiple readers, though if there are multiple writers you'll need to use some kind of file locking or similar method to ensure concurrent writes don't step on each other. Or, use a database!

In the past, the same could be achieved with CGI with its CGI::HTML::Functions. However, since those aren't a best practice anymore, I would not recommend it!

#!/usr/bin/perl use warnings; use strict; use CGI qw/:standard/; use File::Replace qw/replace3/; use JSON::PP qw/encode_json decode_json/; # NOT RECOMMENDED, use an alternative like Mojo instead! my $DATA_FILE = '/path/to/data.json'; my $defaults = decode_json( do { open my $fh, '<', $DATA_FILE or die "$DATA_FILE: $!"; local $/; <$fh> } ); print header; print start_html('Hello, World!'); if ( param('Submit') ) { my (undef,$outfh,$repl) = replace3($DATA_FILE); print $outfh encode_json { name => scalar param('name'), }; $repl->finish; print p( "Form submitted!" ); } print start_form; print div( label({-for=>'name'}, "Your Name"), textfield('name', $defaults->{name}) ); print div( submit('Submit') ); print end_form; print p( a({-href=>url(-absolute=>1)}, 'Reset') ); print end_html;

Update just in case I made it sound too much like these are the only options: of course there are others, see e.g. here, here, and hippos's post here. Also:

* If atomic renames are supported by the OS and FS. If not, some other technique will be necessary. /Update

Unfortunately people here think every question is stupid, and no longer wish to help other people like they used to do. They only want to downvote questions from people trying to learn and ask for help when something is confusing, instead of trying to help them.

It's a FAQ: Why did I get downvoted?


In reply to Re: CGI HTML Form Data by haukex
in thread CGI HTML Form Data by johnfl68

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.