Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2022-05-18 23:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (71 votes). Check out past polls.

    Notices?