Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Switching from PHP to Perl/Mason

by erickp (Sexton)
on Feb 06, 2023 at 20:36 UTC ( [id://11150199]=perlquestion: print w/replies, xml ) Need Help??

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

Hi guys,

I've recently came back to Perl after "trying" to use it over 20 years ago. Yes I've wasted 20 years of good stuff, that happens.

In the past I mainly used PHP for my web work. Even though I'm pretty fluent in PHP, I would love to switch to Perl instead.

I've found Mason/Mason2 so I can use it pretty similarly than I used PHP, easier learning curve for me it seems.

I've also tested the speed of PHP vs Perl with a simple "print" statement looped 1000 times, some of you may have seen this somewhere online. I was shocked that the Perl5 version ran about 38 times faster than PHP7. Now I'm sure this doesn't mean Perl is faster in every context but still, quite encouraging to me as far as performance goes.

My question to you guys is :

If I want a simple Perl replacement for PHP, is Perl/Mason the best way to go? So far it seems like a really good alternative. I just want to embed Perl tags like PHP does. I don't need anything else too fancy.

I've seen frameworks like Dancer2 and Mojolicious around, but as great as these seem, I'd rather use vanilla Perl do my own, my needs are quite small, I'd rather not use a framework if possible.

Any comments/ideas?

Thanks again.

Replies are listed 'Best First'.
Re: Switching from PHP to Perl/Mason
by alexander_lunev (Pilgrim) on Feb 07, 2023 at 09:30 UTC

    Hello and welcome (back)!

    If someone asks me to rewrite PHP web site to Perl, I would start with data that needs to be entered, processed and displayed, and enclose all data work in a module. It would be simpler to write, maintain and test this data module alone without presentation stuff. Suppose you already have some middleware (how would you process HTTP/CGI requests - with web server like apache/nginx, I guess), and then to presentation: if it's just a page with simple HTML, which is used just to get parameters from forms and process it - you can use your data module in a simple CGI script. If your old PHP site was driven by many .php files, I would definitely look at some framework (I'd suggest Mojolicious here), so you can process requests, parameters and routing in a framework, and still keep your data logic in your module, while presentation could be simple HTML (all framework have templates for this), unless you need a complete front rework to an interactive site - then your path would lie in a valley of JS (which is not bad at all, if you ask me).

    The main idea is a separation of concerns, when you can choose right tool for right thing, so in the future, when you would need to rework again this site to some new presentation, or logic, or middleware, you would just pull one part and replace it with another. You may rewrite your data module in another language, or you can replace HTTP/CGI framework to some command line interface, etc.

Re: Switching from PHP to Perl/Mason
by Discipulus (Canon) on Feb 07, 2023 at 11:42 UTC
    Hello erickp and welcome to the monastery and (back!) to the wonderful world of perl!

    as for templete systems for perl ( alongside with Template::Toolkit ) I have heard great things about Text::Xslate

    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.
Re: Switching from PHP to Perl/Mason
by erickp (Sexton) on Feb 06, 2023 at 21:53 UTC
    Actually, just found out I can simply "print" or "say" whatever html I want straight out to the browser...I don't think I'll need any libraries to help me do that. Pure vanilla Perl, love this. Thanks.
      I can simply "print" or "say" whatever html I want straight out to the browser

      You can indeed. You can even do it with Here Documents and there are times when that is appropriate...

      But - and it's a big but - it would be time well spent to get to grips with one of Perl's many templating systems. Thanks to The Monastery, I took the plunge a couple of years back and have no regrets for the time invested. Personally, I use Template but there are plenty of others to choose from.

      If you grow beyond your current requirements, say more complicated use of HTML/CSS/JS you may want to consider a templating engine or framework. I use Mojolicious, it has a powerful and easy to use rendering methods which make life so much easier.

Re: Switching from PHP to Perl/Mason
by Anonymous Monk on Feb 07, 2023 at 14:43 UTC
    I just want to embed Perl tags like PHP does.
    I'd rather not use a framework if possible.
    I want a simple Perl replacement for PHP.
    This is what the OP wants. It may not be what you want, but it is what the OP wants. Give people what they want:
    1. Embed Perl tags.
    2. Not a framework.
    3. Simple.

    Markup::Perl

    use Markup::Perl; <html> <body> <p> Your "lucky number" is <i><perl> print int rand 10 </perl></i> </p> </body> </html>

    Dear Monks,

    Please do not downvote and censor this node for giving OP a correct answer to the question they asked about a capability they want from Perl. That attitude is why something as bad as PHP utterly devastated Perl's dominance of web programming. Here we are a few decades later and you guys still refuse to give people what they want!

    Love, Anonymous

      Dear Anonymous Monk,

      > Please do not downvote and censor this node for giving OP a correct answer..

      Actually I see Reputation: 9 (no significant downvotes) and this is a normal trend here nowadays. Perhaps a decade or two ago it would be something like Reputation: 50 (-5) but here we are..

      The smaller community has its advantages: lesser flames and trolling: I see downvotes only on blatantly bad answers and yes, we still have some bad contributors, but the medium level of partecipation is very hight. I'm not speaking of me, of course.

      > .. and you guys still refuse to give people what they want!

      Here I dissent. The complex process of understanding and the trip of knowledge is not built on: I want a so give me a but more like: well you claim to wont a but do you know about à or A ? With children your best is to offer a wide panorama, not a narrow path to follow, so almost every advice is welcome and no, there is not the true answer (apart 42 obviously ;)

      I abandoned many years ago the idea of leading people through a specific path. By far better to offer your own look of the panorama and let them choose or even invent their own one. We all assume erickp being adult and already a programmer. Maybe they see Text::XSlate and it opens their mind, maybe not: who are us to judge or forecast something? Just offer your contribution in a good way. Stop.

      With love too.

      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.
Re: Switching from PHP to Perl/Mason
by erickp (Sexton) on Feb 07, 2023 at 16:39 UTC
    Thanks for all your answers guys, gives me stuff to chew on, I appreciate it.
Re: Switching from PHP to Perl/Mason
by karlgoethebier (Abbot) on Feb 07, 2023 at 06:26 UTC
    «…vanilla Perl…quite small…»

    If that's the case, take a look here. Smaller hardly goes. Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-24 06:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found