Perl vs. PHP

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
Revision as of 16:45, 20 October 2006 by Woozle (talk | contribs) (→‎Links: OOP comparison)
Jump to navigation Jump to search

computing: software: Perl/PHP: Perl vs. 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 may be more powerful, but it's not at all clear how.) A comparison:

characteristic Perl PHP
class files one class per file 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 Perl, it's not quite clear what's going on with the field names. Is FIELDNAME implicitly created as a constant in this context?
  • 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).
  • I still don't understand what bless does. (Woozle) Or shift.

Notes

  • PHP::Interpreter: a Perl module for running PHP code, thus making it easier to gracefully port a project from one to the other