Perl VS C++

download Perl VS C++

of 1

description

perl

Transcript of Perl VS C++

There are a number of significant differences between Perl and C++ in their approach to object-orientation. Object structure. C++ requires you to declare your object's structure using theclasskeyword, unlike Perl, which doesn't care how you keep your object's state- as a hash, array, or scalar. The only thing Perl really asks is that you return a blessed reference to that data. Privacy. C++ has keywords to enforce various shades of privacy (private,protected,public). Perl does not enforce privacy; if you need privacy you can use lexical variables. Constructors/destructors. C++ requires that the constructing subroutine of an object have the same name as the class. Perl doesn't have any such strictures- any subroutine can be a constructor (the namenewis just a convention). On the other hand, when an object is going to be destroyed, both Perl and C++ require well-known destructor names. A C++ constructor is really an initializer; the memory is allocated before the constructor is given control. In Perl, it is the programmer's responsibility to do both allocation and initialization. Static versus instance methods. C++ provides thestatickeyword to distinguish between static functions and object methods. Perl doesn't make that distinction- subroutines are indistinguishable from each other. A Perl subroutine can examine its arguments and can act as either. Declaration and definition. C++, unlike Perl, requires that declaration of a class be independent of its implementation (unless the implementation is inline). The typical C++ convention is to put the declarations in a header file and the implementation in a separate file. Compile-time versus run-time features. C++ requires that all class information, such as the inheritance hierarchy, the number and type of attributes and methods, and so on, be known at compile-time. Perl allows a run-time redefinition of everything; you can add, delete, or update methods or change the inheritance hierarchy by changing@ISA. I recommend that you not take advantage of this ability. Run-time binding. Since C++ does strict type-checking, run-time binding works only if the objects inherit from a common base class. Perl doesn't have this restriction.