Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Small Code Snippet from PHP to Perl

by sundialsvc4 (Abbot)
on Nov 06, 2013 at 16:08 UTC ( [id://1061441]=note: print w/replies, xml ) Need Help??


in reply to Small Code Snippet from PHP to Perl

Start by very-carefully reading DBI, which is the most-commonly used database interface unit in Perl.   Patiently read this entire document, top-to-bottom.   Rinse and repeat.   Then, spend several more hours today and tomorrow, surfing the web for tutorials.

The biggest difference between PHP and Perl, in this regard, is that with PHP “everything that you can reasonably get to” is compiled into the PHP executable at the time that it is built.   (Which is one reason why, on my system, /usr/bin/perl is a cool 86,000 bytes long, whereas /usr/bin/php is 32.2 megabytes!)   With Perl, you would “use DBI;” in order to gain access to what goodness that module gives you, and that module must be installed on your system (as, in this case, it undoubtedly is).   The core Perl language-interpreter is extremely small, and the system is extremely modularized.   Neither approach to programming-language design is “right” or “wrong,” but each must be clearly understood.

Then, as you can see from the examples in DBI and the tutorials that are out there, it’s pretty basic stuff ... but it is all being done with objects and methods, not built-in functions.   A connect() call gives you a connection to a database and a “connection handle,” which represents another Perl object that has methods such as prepare and execute ... each of which return a “statement handle” (yes, another object ...) that represents the results provided by that statement.   The methods that are available are, of course, very comparable:   mysql_fetch_assoc() corresponds to $hash_ref = $dbh->selectrow_hashref($statement), and so on.   But none of it is “built-in” to Perl.   PHP keeps a lot of internal state-information about its database connections within its own “guts,” and does not necessarily expose them to you, whereas in Perl nothing of this sort is internal to the interpreter ... it’s all done through these objects, none of which are (nothing ever is ...!) “built-in.”

You’ll hear this a lot:   TMTOWTDI .. “Tim Toady” .. There’s More Than One Way To Do It.™   You can easily see that this is the case, just in looking through the list of methods in DBI.

Another thing-to-know about Perl is that you basically must add two statements to your program at the top:
    use strict;
    use warnings;


... because, without them, Perl will try rather mightily to “do what it thinks you want,” and will not tell you about various syntax errors and so-forth.   If you don’t know to expect that behavior, you might not realize that an error had been made, and that the error is adversely affecting your code.

If you are using a “PHP-aware” text editor now, such as Eclipse, then you should install similar Perl-language support into it, as well.

You will find, as you get to know the Perl system, that a lot of it will be very familiar to you from PHP:   there aren’t that many “new ideas” in this world, after all.   Hashes, lists, arrays, classes ... “the usual suspects” ... yet implemented in a very different way.   Importantly, the notion of “mixing HTML with executable code” is absolutely non-existent.

Log In?
Username:
Password:

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

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

    No recent polls found