Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

PHP to PERL ?

by brendgard (Novice)
on Dec 27, 2005 at 23:00 UTC ( #519443=perlquestion: print w/replies, xml ) Need Help??

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

We have been tossing around the idea of upgrading part of our site from a PHP script to one written in PERL. Part of what's holding us back is my almost total lack of PERL experience.

I do have one question that is probably the biggest I have, but is probably so insignificant to you that you'll laugh. PHP has require() and/or include() that allow me to change the contents of one file and it changes what's displayed in the page(or pages). For example, I insert

require(anouncments.txt);
into the main page, and to change any news for that page, I only need edit the announcments.txt file instead of the page being displayed.

It also allows me to add code(such as an RSS feed).

I'm sure there has to be the same setup for PERL, I just don't seem to be able to find it. Anybody point me in the right direction? I don't even have an idea of where to looks in the books it would seem, what question to ask.

Thanks.

Replies are listed 'Best First'.
Re: PHP to PERL ?
by runrig (Abbot) on Dec 27, 2005 at 23:09 UTC
      Well, some of what I'm wanting looks like it will be covered by that. However, some doesn't. Part of what I'm looking to do is call extra code from a seperate file. For example, the section we are looking to change over has a seperate file called by the main file that defines all the SQL section. Another file we are looking at is called to handle the code needed to change an RSS feed into HTML. That's part of what I'm looking to learn about as well. Is that part of where you pointed me? Maybe I'm just not seeing it. Thanks for the replies.
Re: PHP to PERL ?
by vagnerr (Prior) on Dec 27, 2005 at 23:07 UTC
    PHP is more tightly integrated into the actual html output than Perl, you will want to look at some of the templating modules available on CPAN such as HTML::Template


    _______________________________________________________
    Remember that amateurs built Noah's Ark. Professionals built the Titanic.

        Just to clarify, your definition of 'better' in this example is probably defined as 'faster'. Note that the author of HTML::Template::Compiled Tina Mueller mentioned on the mod_perl mailing list that she wouldn't advise using H::T::C in production.

        Not to worry though because if speed is your goal, you might want to have a peek at HTML::Template::JIT instead. It's stable and has a decent test suite.

        -- vek --
Re: PHP to PERL ?
by TedPride (Priest) on Dec 27, 2005 at 23:45 UTC
    What you're probably looking for is:
    do 'mycode.pl';
    As for inserting data into an HTML page, Perl prefers to keep its code separate from text. So instead of dropping code snippets here and there in the page, you'd do something more like this:
    use strict; use warnings; my ($handle, %hash); ###### Collect template contents and change to string open($handle, 'template.dat'); $_ = join '', <$handle>; close($handle); ###### Some dummy values to insert in the template $hash{'name'} = 'Theodore'; $hash{'age'} = 26; ###### Every instance of %hashkey% is replaced with ###### the corresponding value { no warnings; s/%(\w+)%/$hash{$1}/g; } print;
      Does that call extra code from another file then transfer control back to the main page that called it? If so, thanks a lot, that's probably the biggest part of what I was looking for. I can probably find that word in the tutorials now that I have an idea of what it is I'm actually looking for lol. Thanks again.
Re: PHP to PERL ?
by ph713 (Pilgrim) on Dec 28, 2005 at 02:54 UTC
    The only real hurdle in switching from php to perl is to wrap your head around the inversion of quoting. php says:
    <html> <body>This is a <?php echo $file_type ?> file </body> </html>
    perl says:
    #!/usr/bin/perl -wT print qq{<html><body>This is a }; print $file_type; print qq{ file</body></html>};
    Which is of course uglier. php is code embedded in html - in perl it has to be html embedded in code. The answer is, as mentioned, to use templating to turn things back around to a more php-ish perspective. But get used to the idea that the template is *not* your code. It is just a template, which may contain some dynamic variables and perhaps even simple decisions and output switching. But any reasonably complex code that doesn't deal directly with the HTML formatting of the output belongs in the real script which then loads and processes the template.
Re: PHP to PERL ?
by johnnywang (Priest) on Dec 28, 2005 at 07:14 UTC
    Someone has to ask you this: why are you moving to perl if your site is already working in php, and you know php and don't know perl? Is there something you want to do in php but can't, and think you can do it in perl?
      Welllllll, to be honest, features and functionality. I'm not the best programmer in either. I've learned enough PHP to make some changes to the PHP script, but I can't add what we want to the PHP script now running the site's job board( websitegurus.us/freelancers ), partly due to a lack of knowlege, mostly because we would need permision/help from another site we want to tie into ( scriptlance.com ). If we buy the PERL script they sell, the help and permision is included.

      We are trying to pull together a number of feeds from various site to make it easier for people looking for work. In this case, changing from PHP to PERL simply makes it easier, plus adds a few features not in the script we already have running. I'd need to learn how to add some text boxes for anouncements etc, some RSS decyphering code for job leads from other sources, rotating ads, and a few other things.

      A side note, if anybody has any job feeds from other sites, we'd love to know about them and maybe add them.

Re: PHP to PERL ?
by TedPride (Priest) on Dec 28, 2005 at 00:04 UTC
    It runs the code as if it were inside the file calling it, yes.
Re: PHP to PERL ?
by Arunbear (Prior) on Dec 28, 2005 at 10:26 UTC
    As you will have realized by now, there are many ways to do this with Perl. However as you are migrating from PHP, I would suggest that you would enjoy a smoother transition by using one of Perl's 'code in HTML' frameworks.

    There are a number of mature solutions to chose from here e.g. Mason, Apache::ASP or EmbPerl

    I'm quite fond of Mason - there's a free book available; e.g. to include your anouncments file, you could just do:
    <!-- other HTML --> <& anouncments.txt &> <!-- more HTML -->
Re: PHP to Perl ?
by b10m (Vicar) on Dec 28, 2005 at 08:32 UTC

    If you want to switch from PHP to Perl, you probably have to change the way you program/script. Unlike with PHP, Perl people like to seperate code and design issues by means of templates (yes, the PHP community has Smarty too now; funny, a templating language for a templating language ;-)

    Instead of dumping your code in between your HTML output, you could just push the variables (output of the code) towards a template. The big advantage is that the result will be more readable, and easier to maintain.

    I tend to enjoy Template Toolkit. To answer you question (how to insert a text file) based on this templating system:

    [% INCLUDE announcements.txt %]

    The learning curve might be a little steep, yet it's worth it, and it pays off, to continue reading the documentation.

    You could also use PLP, by good fellow monk Juerd.

    --
    b10m

    All code is usually tested, but rarely trusted.
Re: PHP to PERL ?
by sh1tn (Priest) on Dec 28, 2005 at 05:02 UTC
    PHP is just a template system. IMHO. The Viewer (from MVC point of view) is quite a rich choice - Template systems.


Re: PHP to PERL ?
by TedPride (Priest) on Dec 28, 2005 at 07:44 UTC
    And I quote from that other page:

    "Sometimes, a module just isn't logical. For example, when you want to use an external configuration file... This is where do comes in."

    Part of what he wants to do sounds like including a table definition, which would be config, not code.

    Incidently, there are a number of things which are far less painful to do in Perl than PHP. For instance, sorts of nested structures, or regex. Anything involving a lot of data processing is much faster to code in Perl. I generally use PHP for less complicated functions, and Perl for admin stuff.

      Part of what he wants to do sounds like including a table definition, which would be config, not code.

      I got the impression that he mostly wanted to include text/html and some external code (e.g. functions). I saw the part about using do for config purposes in that other page that I linked to, which is why I said "in general" don't use it. I assumed that if he read the node that he'd get the idea. I've seen do abused too many times (post perl 4) to just say "here's how do works, go ahead and use it!" It's better to give context and better alternatives when possible.

Re: PHP to PERL ?
by Delusional (Beadle) on Dec 28, 2005 at 11:03 UTC
    Just as an example:

    Script file
    my $file = "somefile.txt"; print "Opening and displaying $file:\n"; include($file); print "Complete\n"; exit; sub include { my $include = $_[0]; open(INFILE,"<$include") || die "Can't find or open $include\n"; @FileContent = <INFILE>; close(INFILE); foreach $line (@FileContent) { print $line; } }

    somefile.txt
    Line 1 Line 2 Line 3 Line 4 Line 5 Line 6 Line 7 Line 8 Line 9 Line 10

    Again, just as an example, and not exactly 'perfect'. As mentioned, there isn't any one way to do anything in Perl. This example, assumes that the file passed to the sub (function) include does not contain any program code, rather only information to display (which can be html code).

    So, assuming your going to use this, you would copy the sub include to some file you include in your file/script, or in the primary file/script. Then place a call to include(), passing a single command, that being the file name to open, and display. Thus, the include() could be use in several locations, with each call to it having any file name you desire.

    But as I said, this is just an example, and I should point out, not really clean. However, it does execute and displays 'correctly'. The output of this example:
    Opening and displaying somefile.txt: Line 1 Line 2 Line 3 Line 4 Line 5 Line 6 Line 7 Line 8 Line 9 Line 10 Complete
Re: PHP to PERL ?
by JSchmitz (Canon) on Dec 28, 2005 at 13:30 UTC
    s/PERL/perl

      s(s/PERL/perl)(s/PERL/Perl/)

      You started it... :-)

        hehe yeah okay you got me = )
Re: PHP to PERL ?
by techcode (Hermit) on Dec 28, 2005 at 12:37 UTC
    Well there is no easy answer on that question. As you noticed you basically asked several questions.

    The main thing you need to realise at this point is that Perl is very much different (in logic) than PHP. Ah also, CPAN ... guess you already heard about it?

    Also, I believe that you would benefit from using a framework. There are many, but my recommendation is CGI::Application. It's quite simple to understand, doesn't abstract too much, doesn't get in your way but gets you on the right track as it promotes using of good coding style and strategies.

    CGI::Application Wiki

    You might also want to check this recent node .... 518869 (CGI or mod_perl) as it gives info on related stuff.


    Have you tried freelancing? Check out Scriptlance - I work there.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2022-05-18 06:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (68 votes). Check out past polls.

    Notices?