Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Migrating from PHP to Perl for the web.

by KurtSchwind (Chaplain)
on Dec 01, 2007 at 02:37 UTC ( [id://654252]=perlquestion: print w/replies, xml ) Need Help??

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

Without starting some type of CGI jihad, I'm hoping that the perl monks can point me in the right direction.

I've been writing LAMP applications for around 10 years now. I'm very comfortable with PHP. I can write a simple PHP application in a few minutes.

During this time, I've been doing more and more with perl, but only at the batch processing level. CLI stuff.

I realize I'm getting a lot of input from the choir, but I really think I'm ready to start a pretty large migration of all my PHP applications to perl CGI. But I'd be lying if it wasn't a little daunting, but it seems that it may be worth it. It certainly would be nice to use modules I've made and use on the command line in some web apps.

I haven't had great success in finding information about this type of migration. What are the potential gotchas and whatnot. Currently I run my own apache web server but I'm thinking of going with a web host. Is it going to be harder to find a host that supports perl?

Some information I've found so far TheSiteWizard : PHP vs Perl doesn't really seem to talk about perl much at all. And NetConcepts : PHP vs Perl seems to lean away from perl for web development, but I'm not sure I buy the reasons given. At least not today. This is from 2002 and I think it may be a bit dated. If anyone could point me in the right direction or perhaps share their conversion success story (or nightmare) I'd really appreciate it.

Update: from what perrin asked - Most of my own stuff isn't using MVC, but I have at times, rolled my own. I'm not a fan of templates like Smarty (mostly because I find them overkill for my needs). I'm not an OO purist. I sprinkle it in where I think it makes sense. I use mod_php in apache servers currently. Does this mean I'll be using mod_perl?

--
I used to drive a Heisenbergmobile, but every time I looked at the speedometer, I got lost.

Replies are listed 'Best First'.
Re: Migrating from PHP to Perl for the web.
by perrin (Chancellor) on Dec 01, 2007 at 03:15 UTC
    It would help if you can tell us a bit about how you use PHP and if you want to use Perl in a similar way.
    • Do you use an MVC framework for PHP, or just in-line your code?
    • Do you use separate templates (e.g. Smarty)?
    • Do you use an O/R mapper for database access?
    • Do you use an OO programming style?
    • Do you use PHP wih CGI, or with something persistent like mod_php or FastCGI?
Re: Migrating from PHP to Perl for the web.
by kyle (Abbot) on Dec 01, 2007 at 04:53 UTC

    I had been programming off and on with Perl for over ten years when I gave PHP a try for about a year. I stopped when I spent a day debugging a problem I wouldn't have had in Perl (caused by PHP's lack of lexical scoping).

    One thing I found somewhat easier about PHP is how it reports errors. If the code I wrote wouldn't even parse, I'd get an error right there in the browser. With my Perl projects, the browser's blank, and the error is in the error log. (Prepare now for a chorus of monks with a solution to this problem, perhaps it's CGI::Carp's fatalsToBrowser, which I've heard of often and used never.)

    One gripe I had with PHP (besides the really galling lack of lexical scoping) is that the language has pretty obviously been put together by people who didn't talk to each other. Functions are named with underscore_style in places, lowercaseAndCaps in other places. There's database functionality built in, but the ubiquitous DBI is superior (in my opinion). You've clearly spent more time with PHP than I have, so I'd expect you to know its warts better than I do.

    I'd suggest taking something (small) you've written before and rewriting it in Perl. I'd also suggest you give the MVC pattern a try. I like it. If the small rewrite goes well, rewrite something large. If that goes well, write something new.

    We'll be here to help. We can argue the technical merits of the languages, but the community is the killer feature.

Re: Migrating from PHP to Perl for the web.
by CountZero (Bishop) on Dec 01, 2007 at 08:03 UTC
    As PHP is directly included in your webpages, it is more akin to working with a templating framework in Perl. Examples are HTML::Embperl, Template Toolkit or HTML::Template and such.

    An MVC framework such as Catalyst makes extensive use of this. Perl does all the heavy (computational and database) lifting, the template allows you to write your webpages quite easily and an OO-wrapper around your database makes for an easy and consistent access to your data.

    At first --and to many a PHP programmer-- this seems unnecessarily complex, but actually it makes for far cleaner code. Changing the data-store is done only in the model-layer (which is mostly just some config-files), the business logic is found in the Perl code in the controllers and the eye-candy for your web-page is found in the view layer of the templates. You will be hard-pressed to maintain such a nice separation of concerns in your PHP-application.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Another technology that you might seriously consider is the use of Mason. This is an open-source handler for Apache that interprets web pages much the same as PHP, and looks for embedded code.

      Mason can be quick if it uses mod_perl as pages are cached as compiled objects in memory. Mason offers a debugging mode that displays compile errors to the web.

      There are programming purists who don't like Mason, but I've used it professionally and found it a good compromise between developing CGI applications quickly and good web performance.

      Have a look at the brief Mason Introduction - it offers the following example of a Mason-interpreted file:

      % my $noun = 'World'; Hello <% $noun %>! How are ya?
Re: Migrating from PHP to Perl for the web.
by tachyon-II (Chaplain) on Dec 01, 2007 at 04:34 UTC

    If the task is getting the job done and you can do that with PHP why swap?

    If you want to do stuff with Perl and PHP together you already can see this

    Translating working code from one language to, by definition, untested, not working, not even written code in another language needs a good reason. I am not reading one.

    You do need to use mod_perl if you want speed comparable to mod_php as vanilla Perl CGI is slow.

      I never even knew that PHP::Interpreter existed! This might very well ease any transistion I make.

      I'm curious as to any limits I might run into if I ran mod_perl as opposed to perl CGI. Do I loose anything on that front?

      --
      I used to drive a Heisenbergmobile, but every time I looked at the speedometer, I got lost.

        mod_perl does not really impose any limits as compared to vanilla Perl - in fact it lets you get up close and personal with Apache if you want.

        What you do lose with mod_perl as say compared to vanilla Perl is the ability to write sloppy, poorly structured code. I recommend reading this this from the mod_perl documentation before you read anything else. There are a few traps, some simple and some fascinating/excruciating but you need to use mod_perl to get the same performance as mod_php. http://www.linux.com/feature/55807

      Before you jump up and down on PHP::Interpreter, ensure that the thing even compiles on the platform in question.

      I recently tried myself (on Ubuntu 7.10) without luck.

      --
      Andreas
Re: Migrating from PHP to Perl for the web.
by sh1tn (Priest) on Dec 01, 2007 at 13:48 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-03-29 04:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found