Difference between revisions of "Perl vs. PHP"

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
Jump to navigation Jump to search
(→‎Links: PHP::Interpreter)
m (tweaks)
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
[[computing]]: [[software]]: [[Perl]]/[[PHP]]: [[Perl vs. PHP]]
+
==Navigation==
 +
{|
 +
|-
 +
| colspan=2 rowspan=2 valign=middle | [[computing]]: [[software]]: || [[Perl]]: || rowspan=2 valign=middle | [[Perl vs. PHP]]
 +
|-
 +
| [[PHP]]:
 +
|}
 
{{seedling}}
 
{{seedling}}
 
==Overview==
 
==Overview==
Line 7: Line 13:
 
==Links==
 
==Links==
 
* [http://tnx.nl/php PHP in contrast to Perl]
 
* [http://tnx.nl/php PHP in contrast to Perl]
 +
* [http://www.cs.wcupa.edu/~rkline/perl2php/ Perl/Php Translation]: a handy reference for how to do useful stuff in either language, even if you're not trying to port from one to the other
 +
==Object-Oriented Programming==
 +
[[object-oriented programming]], or OOP, was a late addition to both languages, but it's a bit easier to use in PHP. (Perl's implementation is being dramatically improved for Perl 6, in development as of January 2007.) A comparison:
 +
{|
 +
|-
 +
! bgcolor="#eeeeee" | characteristic || [[Perl]] || [[PHP]]
 +
|-
 +
| bgcolor="#eeeeee" | class files || typically one class per file,<br>but multiple classes are possible || multiple classes per file
 +
|-
 +
| bgcolor="#eeeeee" | class initialization code ||
 +
my $self  = {};
 +
$self-&gt;{'FIELDNAME'} = ''default'';
 +
...
 +
{{perlfunc|bless}}($self);
 +
return $self;
 +
|
 +
$this-&gt;fieldname = ''default'';
 +
|-
 +
| bgcolor="#eeeeee" | initialization code within methods || my $self = {{perlfunc|shift}}; || ''none needed''
 +
|-
 +
| bgcolor="#eeeeee" | identifier within class code || '''$self->fieldname''' ||
 +
* '''this->$fieldname''' for per-object data
 +
* '''self::$fieldname''' for static data
 +
* '''this->method()''' for calling method dynamically
 +
* '''self::method()''' for calling method statically
 +
|}
 +
===OOP Notes===
 +
* In class methods, the object itself is an implicit first argument; the {{perlfunc|shift}} function uses the @_ magic variable by default, effectively retrieving the object reference.
 +
* The Perl {{perlfunc|bless}} function causes the package's functions to be bound into the [[hashref]] object (probably creates a [[virtual method table|VMT]] for it); without being blessed, the object is just a normal hashref.
 +
* On the surface, PHP's identifier-within-class syntax seems more complicated, but it made intuitive sense to me ({{woozle}}) except possibly for the issue of which identifiers get dollar-signs and which don't (which is even more complicated in Perl)... although I don't see why PHP needs both "this" and "self" when there's the "->" vs. "::" to disambiguate dynamic from static (but again, this is a minor annoyance and much less confusing than many aspects of Perl's OOP syntax).
 +
* In Perl, any string inside delimiters (such as [], {}, ()) is assumed to be a string literal if it does not begin with $ and does not contain any delimiters. (This is a rough guess at the actual rules, which may be more complicated.) For this reason, some examples of class initialization show the field names without quotes, e.g. "$self->{FIELDNAME} = 'value';", which can be more understandably written as "$self->{'FIELDNAME'} = default;".
 +
* Apparently the OOP syntax in Perl 6 (currently in development) is much improved and more intuitive; the OOP syntax in Perl 5 is more like grafting OOP onto a basically procedural language.
 +
 
==Notes==
 
==Notes==
* [http://search.cpan.org/~gschloss/PHP-Interpreter-1.0.1/lib/PHP/Interpreter.pm PHP::Interpreter]: a [[Perl]] module for running [[PHP]] code, thus making it easier to gracefully port a project from one to the other
+
===Porting from PHP to Perl===
 +
* [http://search.cpan.org/~gschloss/PHP-Interpreter-1.0.1/lib/PHP/Interpreter.pm PHP::Interpreter]: a [[Perl]] module for running [[PHP]] code
 +
* [http://search.cpan.org/~spoon/PHP-Strings/Strings.pm PHP::Strings]: a [[Perl]] module that implements most of the PHP String functions (those which are not implemented are generally trivial, and are stubbed with a message explaining this)

Latest revision as of 17:40, 27 January 2007

Navigation

computing: software: Perl: Perl vs. PHP
PHP:
This is a growing seedling article. You can help HTYP by watering it.

Overview

Perl and PHP are often compared with each other, largely due to their mutual popularity for web site design. The general consensus seems to be that people use PHP because it was invented specifically for creating web pages, whereas Perl has a lot of other uses as well — or, in other words, people use PHP because they don't know any better, and people who know better use Perl.

Unfortunately, this apparently does nothing to prevent the use of PHP in such heavily-used, highly-developed applications as MediaWiki and Drupal.

Links

Object-Oriented Programming

object-oriented programming, or OOP, was a late addition to both languages, but it's a bit easier to use in PHP. (Perl's implementation is being dramatically improved for Perl 6, in development as of January 2007.) A comparison:

characteristic Perl PHP
class files typically one class per file,
but multiple classes are possible
multiple classes per file
class initialization code
my $self  = {};
$self->{'FIELDNAME'} = default;
...
bless($self);
return $self;
$this->fieldname = default;
initialization code within methods my $self = shift; none needed
identifier within class code $self->fieldname
  • this->$fieldname for per-object data
  • self::$fieldname for static data
  • this->method() for calling method dynamically
  • self::method() for calling method statically

OOP Notes

  • In class methods, the object itself is an implicit first argument; the shift function uses the @_ magic variable by default, effectively retrieving the object reference.
  • The Perl bless function causes the package's functions to be bound into the hashref object (probably creates a VMT for it); without being blessed, the object is just a normal hashref.
  • On the surface, PHP's identifier-within-class syntax seems more complicated, but it made intuitive sense to me (Woozle) except possibly for the issue of which identifiers get dollar-signs and which don't (which is even more complicated in Perl)... although I don't see why PHP needs both "this" and "self" when there's the "->" vs. "::" to disambiguate dynamic from static (but again, this is a minor annoyance and much less confusing than many aspects of Perl's OOP syntax).
  • In Perl, any string inside delimiters (such as [], {}, ()) is assumed to be a string literal if it does not begin with $ and does not contain any delimiters. (This is a rough guess at the actual rules, which may be more complicated.) For this reason, some examples of class initialization show the field names without quotes, e.g. "$self->{FIELDNAME} = 'value';", which can be more understandably written as "$self->{'FIELDNAME'} = default;".
  • Apparently the OOP syntax in Perl 6 (currently in development) is much improved and more intuitive; the OOP syntax in Perl 5 is more like grafting OOP onto a basically procedural language.

Notes

Porting from PHP to Perl

  • PHP::Interpreter: a Perl module for running PHP code
  • PHP::Strings: a Perl module that implements most of the PHP String functions (those which are not implemented are generally trivial, and are stubbed with a message explaining this)