11

This is probably a naïve and even not a valid question...

I've made a Perl 6 program (and hope to make a couple others) which analyzes local data taking into account some user-defined parameters. Several of my colleagues/coworkers need the output of the program on a regular basis, so they send me the parameters, I run the program and send them back the output.
Since the whole process isn't very convenient, I'm thinking about what can be done to make things easier for everyone. Here are the options, and neither of them seems ideal.

  • Install Perl 6 on my colleagues' computers and teach them how to use it. Most of them don't have any programming experience, the data and the program itself should be kept up to date on all the computers, the program should work identically on different systems etc., so this will presumably make things worse, not better.
  • Make an online variant of the program (using JS etc.), so that my colleagues can input the parameters and get the result in a browser window. This is much easier for the users (and I've already made such a thing for a simpler program), but I would prefer to use Perl 6...

So, my question: is it possible to somehow make Perl 6 work online? Can it work in the browser? Or is there any other solution?

Eugene Barsky
  • 5,780
  • 3
  • 17
  • 40
  • 1
    Some 20 years ago, Perl was the de-facto standard language for writing (server side) CGI scripts. It lost some of its popularity to PHP and other web application frameworks. But if you look for "Perl CGI tutorial" you'll find plenty information on how to do it. – datenwolf Sep 23 '18 at 19:24
  • 3
    You probably want to take a look at Cro, it's quite a complex question though. – Scimon Proctor Sep 23 '18 at 20:24
  • @Scimon Thanks! I've looked through some docs and examples and because of my ignorance probably JS will be a more simple solution... – Eugene Barsky Sep 23 '18 at 21:17
  • 1
    If you want the whole thing to take place in the browser, JS could do the work. If you want a browser portion that communicates back to a server that does the real work, consider JS for the browser portion and Cro/Perl6 for the server portion. – Curt Tilmes Sep 24 '18 at 00:31
  • @CurtTilmes Thanks! Is there a Cro tutorial for beginners like me, who have never worked with any server-side language? – Eugene Barsky Sep 24 '18 at 07:17
  • 1
    @EugeneBarsky Did you see https://cro.services/docs/intro/spa-with-cro? – raiph Sep 24 '18 at 09:24
  • @raiph Thanks! Yes, I did. Unfortunately, even this is rather complicated, since I didn't have any server experience. – Eugene Barsky Sep 24 '18 at 10:25
  • 1
    Eugene, I've updated my answer in a couple ways, mostly to address your point about inexperience with a server. – raiph Sep 24 '18 at 13:08

3 Answers3

12

Install Perl 6 on my colleagues' computers ... this will presumably make things worse, not better.

Given your stated details for the ... I agree.

Make [a] variant of the program (using JS etc.), so that my colleagues can input the parameters and get the result in a browser window.

This is a natural choice imo.

(Note that I elided "online" in my quoting of your words. If your colleagues are able to see your computer via an internal network, then you can still do a JS/web solution but do one that's not public -- not online. Basically do the same solution but use slightly different server plumbing.)

An alternative would be to accept a structured email. More on that in the appropriate spot.

I would prefer to use Perl 6...

The most common way to use code from a language other than JS in a web solution is to still use html etc., quite often including JS, in the "front-end" of the solution and then include calls of code running on a server as the "back-end".

(The other way is to transpile to JS. In theory you could transpile Perl 6 to JS via an experimental nqp backend. I think this is an exciting development but I suspect it will be slow and limited in the near term.)

(One final point for this section. If instead you end up translating the Perl 6 to a JS solution because you don't want to learn or deal with having a server in the mix, then you'll have used Perl 6 as a prototyping language. This can be a legitimate way to use a language but the rest of this answer assumes you'll introduce a server.)

Introducing and managing a server

A server is a computer or software that reliably stays available and runs software on demand due to requests from "clients" (typically other computers).

That sounds simple, and in some ways it is, but in other ways it isn't. What if someone turns off the power? Or fails to pay the internet bill? Or the software leaks memory causing the system to periodically fail? Or a hacker attacks and gets in?

Unless those using the service can already see the server computer over a local network, and often even then, a modern approach to introducing a server into a solution is to purchase an online server package (typically one based on virtualization). (Think in terms of a few dollars a month and up.) Assuming a decent service provider, this effectively guarantees it'll be a well run server that generally stays on, working, connected, and serving unless it's hacked or otherwise broken, and that you'll be informed in a timely fashion if the latter happens. The "zero cost" alternative is to make your desktop be a server too. That means you're responsible for keeping your desktop up and running and connected.

A modern approach to managing a server is to use Docker or similar on top of the server. This effectively guarantees you can near instantly fix a problem with a well run server if it's hacked or otherwise broken. It also makes it easy to have a local server that you can develop and test on that's guaranteed to be identical to the deployed production server. Best of all perhaps, installing Docker on your desktop or using it on a purchased server and then dropping in an already prepared docker file means you can can go from never having used a server to having a working server in the time it takes you to click a few buttons.

There is a range of offerings out there that use both OS virtualization and docker or docker like technologies to create pay-for-use combinations where you only pay for the time the server is in use rather than for it being available 24/7. There are many ins-and-outs. Some don't support Perl 6. As far as I know Amazon AWS and Google Cloud both support pay-for-use and allow use of any programming language.

Building the web application

The final few sections of this answer cover how to use Perl 6 once you have a server in place, starting with the least sophisticated. Jump to the last section, on Cro, if you want to go straight to the solution I recommend if you go the web route.

Email client responder

Especially if you don't want to spend the money on a separate server, to simplify things a little, you could have your colleagues send a structured email to an email address that arrives at an email client you have running on your desktop, which responds by running the Perl 6 program on your desktop and then emails them back when the results are in. If you're interested in this solution, please post another SO asking about it.

The remaining sections assume a web solution instead.

CGI and hand-rolled code

Calling code running on a server from a web page is trivial.

You just write a suitable link to click on that points to a dynamic program rather than a static html page.

If you want to collect parameters, then you just write a form and a suitable submit button and do it that way.

Given an existing web page with a form already written you could learn how to pass parameters, call Perl 6 code, and display the results, in minutes, if you read an appropriate article such as How to generate webpages using CGI scripts.

Modules and Bailador

Imo it's not worth doing things at such a low level as hand-rolled CGI. There are dangers to doing so and there are modules that make things easier and less dangerous to create and maintain.

These will appear under "web" at modules.perl6.org.

Bailador is an obvious pick for a basic solution that does a decent job with "routes" (mapping URLs to code) and templates.

Cro and an SPA

If you are interested in using the most fundamentally natural approach to building modern web sites, I recommend using Cro. Cro makes it trivial to build simple web solutions using the SPA approach. But it's designed to use the features built in to Perl 6 to scale nicely to the most sophisticated web sites imaginable involving any mixture of asynchronous, concurrent, parallel, or distributed processing and any middleware you care to introduce.

A Cro SPA is possibly overkill for such a very trivial application as you suggest in your OP but it's still very simple and there are countless upsides to choosing it and no significant downsides imo for your application. (Even its official status as a beta product is fine for your scenario imo. It's already a very solid product, devs respond quickly to any issues raised, and it's of strategic importance to both those devs and indeed Perl 6 that it stays clean, fast, production worthy, and well maintained.)

The quickest way to get an entire setup going so you can start playing with Cro is probably to install Docker if you don't already have it on your server (or desktop if you're using that as your server) and then install the croservices/cro-http Docker container.

And the quickest way to learn how to use Cro for delivering a single web page is to follow the Building a Single Page Application with Cro tutorial.

raiph
  • 31,607
  • 3
  • 62
  • 111
  • 1
    Thanks a lot for showing all the available options! Now I see the perspective and know what to learn. :) We don't have any local network, some of my coworkers live in other countries. – Eugene Barsky Sep 24 '18 at 13:17
  • 3
    @EugeneBarsky, I've made more edits including adding pricing and "zero cost" options to the server section, emphasizing starting with docker to that section and Cro section, and adding a brief section on an email approach that would likely be simpler and cheaper. – raiph Sep 24 '18 at 14:55
  • Thanks for the perspective! Now I have a much broader understanding of all the possibilities. – Eugene Barsky Sep 24 '18 at 19:30
  • 1
    *Some* of the possibilities, but they reflect me thinking in an old-fashioned and non-creative way. I love Matt's answer. I recommend you dig into his suggestions before pursuing the ones in my answer. If one of Matt's suggestions works out for your current use case, just consider my answer above a guide if you ever want to create a more general website with Perl 6. – raiph Sep 24 '18 at 22:38
  • Excellent write-up! As an aside, I also enjoyed reading mentions of NQP (Rakudo.js) and Graal, related to the Perl-6-to-JavaScript idea. – Tommy Stanton Sep 25 '18 at 21:31
  • Eugene, It's hard to know if the P6 to JS transpiling project is months or years away from being usable but I'd love to hear how it worked or not for you if you ever try it. The guy who's driving it forward just posted an update but the test 404d for me: http://blogs.perl.org/users/pawel_murias/2018/09/rakudojs-update---running-perl-6-in-the-browser-with-parcel.html @TommyStanton Did you see this latest? :) – raiph Sep 26 '18 at 01:36
  • @raiph What I don't understand in this solution: if I transpile my P6 program into a JS program, how will the input be organized? Where do I put the files with data? – Eugene Barsky Sep 26 '18 at 20:01
  • @EugeneBarsky I don't know. What approach were you going to use if you wrote a JS solution? (I mean that as a rhetorical question, not as a question for you to answer here.) I think you should ignore this approach unless you hear it's usable -- and that may be years. – raiph Sep 27 '18 at 00:06
  • @raiph I can answer this rhethorical question. :) I put all the huge data into `.js`-files, but I don't think it's a good way. – Eugene Barsky Sep 27 '18 at 07:08
  • Commenting [on behalf of pmurias](http://colabti.org/irclogger/irclogger_log/perl6?date=2018-09-30#l621): RE: "which can in turn target JS", As far as I'm aware there are no plans for Graal to target JS, there are vague plan for it to target webassembly (ones it matures and gains extra features) but I'm not aware of an actual effort. – Elizabeth Mattijsen Sep 30 '18 at 19:43
  • @ElizabethMattijsen Thanks and thanks to pmurias and awesome news about 6pad. – raiph Oct 09 '18 at 03:32
  • 1
    @EugeneBarsky See http://blogs.perl.org/users/pawel_murias/2018/10/rakudojs-update---it-has-been-merged-into-master-plus-6pad-unveiling.html (though remember that it's early days -- no one can know how long it'll take to shape up). – raiph Oct 09 '18 at 03:34
  • @raiph Thanks, I noticed this project, but not sure I fully understand how (and whether) it works. – Eugene Barsky Oct 09 '18 at 07:15
  • 1
    @EugeneBarsky "not sure I fully understand how" There are 2 answers to that. **1. The compiler tech.** There's [a new part of the Rakudo P6 compiler](https://github.com/rakudo/rakudo/commit/a09536fd942dd48c969a092549693b1bfa9c4be5) which targets [a JS backend for NQP](https://github.com/perl6/nqp#javascript-backend) which targets Node (a JS runtime) running on browsers. pmurias has got the whole show working in Chrome in a way that will soon work in Firefox and should soon work in Edge etc. **2. `6pad`**. Write P6 in a github gist. `6pad` converts it to a web app. No one need install Rakudo! – raiph Oct 09 '18 at 11:55
  • 1
    @EugeneBarsky "whether ... it works." My guess is, once some bugs are ironed out, the main issue determining whether it'll work well enough for a particular simple use case will mostly be about speed, and for more complex use cases, maturity. pmurias is highly capable and determined. He's been at this for years. He's taken into account both correctness and speed. P6 is coming to the browser and it'll work. It's difficult to predict how long it'll take to mature. But a simple app will either work or it won't. And I'm sure pmurias will guide folk in getting any bugs dealt with or worked around. – raiph Oct 09 '18 at 12:08
7

One nice solution is you could run a Jupyter Notebook server for them to use https://github.com/bduggan/p6-jupyter-kernel

Or another is host your code on glot.io so they can run it in the browser https://glot.io/new/perl6

Those would be my quick solutions. If the program is quite simple and runs on the command line there's a chance the JVM version of Rajudo might pull it off with the --target=jar to package everything up to run on other machines with Java installed.

Matt Oates
  • 778
  • 4
  • 6
  • Thanks! Could you please tell a bit more about Jupyter? I feel I don't quite understand how it words. – Eugene Barsky Sep 24 '18 at 19:37
  • 2
    Eugene, you really should get into Jupyter. It's like a REPL on steroids -- and much more. Let us know if [Perl 6 on Jupyter](https://www.youtube.com/watch?v=tSZV8IXIsM4&index=18&list=PLRuESFRW2Fa77XObvk7-BYVFwobZHdXdK) is helpful. You should also click on the glot.io link in Matt's answer and play for a few minutes just so you know about that too. (The only real limitation for glot.io that I know of is that it's limited to programs that complete in less than 60 seconds.) – raiph Sep 24 '18 at 22:21
  • @raiph Is it possible to upload data files to glot.io, so that the program can use them? – Eugene Barsky Sep 25 '18 at 21:02
  • Look for the words "Text entered here will be sent to stdin" for stdin. (If you have stdout shown you'll need to click the Input button.) You can also add named files by clicking the `+` button next to the `main.pl6`. These can be modules you paste in or just data. They're stored in the same directory. If you write modules in these other tabs you'll need to add a `use lib '.';` to your main.pl6. You can also add command line arguments by clicking the butterfly icon. I suspect glot.io may cap your file space or RAM use so perhaps won't work for you. But try it and let us know. – raiph Sep 26 '18 at 01:15
  • Just bumped into this again. "Is it possible to upload data files to glot.io?" ... (Yes, as explained in my previous comment.) The answer is also yes for other online evaluators, with timeout being one key distinguishing feature for the practicality of using an online evaluator as an online Raku solution (along with other key aspects such as a service being free and/or sustained for years to come). See [my table of online evaluators from about a year ago](https://www.reddit.com/r/rakulang/comments/kj63dq/glotio_now_supports_raku/ggxow6n/), which includes a timeout column, links, etc. – raiph Apr 20 '22 at 14:26
2

Since you have a program implemented that you seem to be happy with, and it is already written in Perl 6, it might be worthwhile to give Perlito a try (for reference, it is listed on the perl6.org compilers page).

The goal would be to use Perlito to do a one-time translation of your Perl 6 source code to JavaScript, which can run in the browser, of course. Maintenance for the program going forward would be a sequence of: (1) Update Perl 6 source code; (2) Run Perlito to get JavaScript source code; (3) Replace old JavaScript source code with the newly-rendered source code.

This whole suggestion is experimental, and I haven't done it myself, so please take it with a grain of salt.

Lastly, there is also a more detailed README (mentioning JavaScript-to-Perl-6).

Tommy Stanton
  • 716
  • 1
  • 5
  • 14
  • I'm trying Perlito on a somewhat-complex Perl 6 that I wrote. It is hanging (from the [demo page](https://fglock.github.io/Perlito/perlito/perlito6.html)) quite badly in Google Chrome. – Tommy Stanton Sep 24 '18 at 00:48
  • This all said, I'm a big fan of Cro as well, which you could use to turn your Perl 6 program into a web service. I have [example source code](https://github.com/tommystanton/hello-world-cro-pm6) and [related slides](https://docs.google.com/presentation/d/e/2PACX-1vTYBu_gMzGPWRZQ1EQol7O9ofoQtpva6uKRHt-0Q9z6YTbKQ7dtcjimO9NWWPyo_51SHnSd-OL4pfrm/pub?start=true&loop=false&delayms=3000). – Tommy Stanton Sep 24 '18 at 01:04
  • Thanks! I've made a `say "Hello world!"` perl6 program, converted it with Perlito, and the result doesn't work in js. :( – Eugene Barsky Sep 24 '18 at 07:12
  • As for Cro, I've installed it, but `cro run hello` gives a lot of errors and warnings in the output and then hangs. Probably I need a tutorial for beginners, if such a thing exists... – Eugene Barsky Sep 24 '18 at 07:30
  • 6
    Perlito6 is an old project. Based on its commit history, it hasn't been updated to match roast, the suite of tests that officially defines the Perl 6 language, since 2012. The reference to it at perl6.org lists it under "Historical Compilers" in a (failing?) attempt to suggest the projects in it are listed primarily for historical interest. http://fglock.github.io/Perlito/perlito/perlito6.html works for `say "hello world";` but I would say perlito currently isn't relevant to ordinary Perl 6 users. – raiph Sep 24 '18 at 08:15