Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Perl Script to run in web browser

by cbtshare (Monk)
on Mar 02, 2018 at 19:22 UTC ( #1210249=perlquestion: print w/replies, xml ) Need Help??

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

Hello All I have written a perl script that automates various tasks, but management needs to access the functionality of that script from the browser instead of having to give me directive to run the script and execute specific functions.The script looks like below when executed via command line:

Please choose one of the following options. 1) Insert all Apps belonging to a group 2) Insert groups in databse 3) Refresh database group table 4) Add all app from one group to another 5) Get Group ID 6) Get User ID 7) Sync groups 8) Get list of all apps 9) exit Please enter a letter or number corresponding to the option you want t +o choose

Based on the selection the appropriate subroutine is called.I would like the menu above and all the output to screen to be shown in the browser. Can you please guide me in the best way to approach this? I was looking at nodejs to accomplish this, but learning curve seems steep, so wanted to know if this can be done in perl Thank you

Replies are listed 'Best First'.
Re: Perl Script to run in web browser
by haukex (Bishop) on Mar 02, 2018 at 19:38 UTC

    There are lots of ways to implement web applications in Perl. These will run on the server and send data to the browser (HTML, JavaScript, etc.), so they don't "run in the browser" in that sense; on the client side you can use JavaScript to make the website dynamic, if needed (for many simple applications it is not). For the Perl scripts on the server side, there are lots of modern frameworks you can use, see for example UP-TO-DATE Comparison of CGI Alternatives. Since it sounds like you're just getting started, you might want to check out Mojolicious::Lite and its tutorial.

Re: Perl Script to run in web browser
by dasgar (Priest) on Mar 02, 2018 at 19:43 UTC

    Sounds like you want a web interface for your code. I would look at one of the web framework modules (such as Dancer, Dancer2, Mojolicious). For the menus (like what your existing code is using), it probably would be easiest to go with regular HTML forms and buttons for that part.

    If you need to distribute your application, you might find useful information in this blog post (Building Perl stand alone applications with a GUI using Mojolicious and PAR-Packer). Otherwise, it may be better to host in the web application on a server that everyone can access via browser.

Re: Perl Script to run in web browser
by tbone654 (Beadle) on Mar 02, 2018 at 22:09 UTC

    Pretty sure the best short term answer might depend on what happens after selecting 1-9. And also, your TAT (turn around time). If there are not a lot of additional inputs required after making a selection, and you wish to dummy something up quickly I often: use cgi;

    There is a pretty good example that used to be in the camel book you can use as a template. I use it all the time, and here is a modified version that you can probably try and run. I think it should work as an example

    #!/usr/bin/perl use CGI qw/:standard/; print header, start_html('test_cgi'), h1('Please choose one of the following options.'), h2(' 1) Insert all Apps Belonging to a group'), h2(' 2) Insert groups in database'), h2(' 3) Refresh database group table'), h2(' 4) Add all app from one group to another'), h2(' 5) Get Group ID'), h2(' 6) Get User ID'), h2(' 7) Sync Groups'), h2(' 8) Get list of all apps'), h2(' 9) exit'), start_form, "Enter a your choice from list above: ",textfield('selection') +,p, "Select which to operate on: ", checkbox_group(-name=>'words', -values=>['users','groups','apps'], -defaults=>['users','groups']),p, "What should we do with the selection ", popup_menu(-name=>'color', -values=>['add','show','update','none']),p, submit, end_form, hr,"\n"; print "$endd \n"; if (param) { print "You selected: ",em(param('selection')),p, "Process: ",em(join(", ",param('words'))),p, "To do: ",em(param('color')),".\n"; } print end_html;
Re: Perl Script to run in web browser -- Dancer2 draft
by Discipulus (Abbot) on Mar 03, 2018 at 11:42 UTC
    Hello cbtshare,

    go learning some modern Perl way to do it! go for a Mojolicious or Dancer2 solution. Here I propose you a draft, just a draft, of a Dancer2 solution using TemplateToolkit too; both are worth to know.

    You can get a barebone working application running dancer2 -a MyWeb::App (see the project page)

    Then you modify config.yaml to use TemplateToolkit (see my example here this was a school project i've done: you can play with it and see other solutions).

    Then under /lib you have the perl module where you'll declare your routes: in a very plain simple way I'd procede having a route for the main page, it's associated template to show. Like in:

    # draft of the module code package YourNameApp; use Dancer2; # this is the root and will show the index.tt get '/' => sub { template 'index' => { 'title' => 'index: choose an action' }; };

    The above will shows index.tt where the string index: choose an action will be used as the content of the template's variable with name title for example, in index.tt you can have a div showing the title:

    <div id="headline"> <% title %> </div> # put here the rest of your HTML, probably a list of link pointing to +other routes, like: # 1) Insert all Apps belonging to a group # pointing to /ins_all_apps

    Then you add such route to your module:

    ... get '/ins_all_apps' => sub { # call your own sub that you can put in this same file my @results = get_reults_of_ins_all_apps; # render the appropriate template template 'results' => { 'descr' => $resutls[0], 'full_text' => $resutls[1] }; };

    Go on.. and have fun!

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2021-10-24 13:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (89 votes). Check out past polls.

    Notices?