Posts Tagged ‘inheritance’

I’m quite a fan of both Java and Perl, for quite opposite reasons; Java is very strict, syntactically, and is rather good in collaborative environments as a result, where Perl is entirely the opposite of strict (even with use strict; in operation), and is therefore a delight to program in.

One thing they share is the ability to work with objects. However, this gives rise to an interesting difference: Java is object-oriented, where Perl is not.

In Java, you call a “function” by using its name, just like in any other language.

// declare the function double square(int x){    return x * x; } //somewhere else, we call it double y = square(x);

Ditto, Perl:

# declare the sub sub square(){    my $x = shift;    return $x * $x; } # somewhere else, we call it my $y = square($x);

All very well, until we get into inheritance.

perl-oo-1
In Java, if the function “square” is declared in a superclass of the calling code, the above snippet will still work perfectly well; if we run double y = square(x); in code in class Banana, it will happily execute square() as if it were a local function.

Perl, on the other hand, is fundamentally a procedural language, so it assumes that function calls as shown above are internal to the current package. This means that if the sub “square” as shown above is in a superclass of the calling code, it won’t work.

This is where we have to _tell_ Perl that it’s using objects.

Firstly, some background. Inheritance in Perl is pretty easy: you create the inheriting package in the usual way, and you populate the @ISA array (pronounced “is-a”) with a list of packages to inherit, with the highest priority/affinity first. Yes, Perl supports multiple inheritance, which Java does not. As an example:

package 'Banana'; @ISA = ('Aardvark');

If package Banana contains the line of code my $y = square($x);, it will cause an error on execution, because Perl doesn’t know that square should be treated as an OO function, but rather it looks in the current scope for that function.

Now, if the calling code above is in a _different_ package, and contained an object reference to our package Banana in a variable $b, then we could get it to work by doing this:
$my $y = $b->square($x);

Interestingly, this will work whether square() is implemented in A or B. Go figure.

So, to get over this, we call the function via an object reference. In Perl, when an instance method is called (e.g. $b->square($x);), the first parameter to the function is the object reference. Conventionally, this is often written into a variable called $self.

If we need to call another function from that one, while retaining OO techniques, we use $self->otherfunction(). This solves our problem with inheritance, and is not a million miles removed from Java’s this keyword, although is far from implicit; remember, Java is fundamentally object-oriented, where Perl needs to be reminded.

Code samples:

package Aardvark; sub square(){     my($self, $x) = @_;     return $x * $x; } package Banana; use Aardvark; our @ISA = ('Aardvark'); sub new {     my $type=shift;     return bless {}, $type; } # test calling an inherited instance method sub printstuff {     my $self=shift;     print $self->square(5.2) . "\n"; } package main; use Banana; my $b = Banana->new(); # see if square works when called from Banana $b->printstuff(); # see if square works when called from here, via Banana print $b->square(1.5) . "\n";
  • chad ochocinco career stats
  • bangles eternal flame mp3bengals forum
  • tea party lies
  • memo
  • vince young redskins
  • search engines for kids
  • randy moss combine results
  • chad ochocinco parents
  • arno
  • hp support quick test pro
  • astronomy
  • new england patriots underwear
  • new england patriots 1997 roster
  • dashboard
  • tea party table settings
  • bea exhibitors
  • c span 4 to 5
  • battleship hacked
  • hyosung
  • tea party zombies download
  • search 3 bodybuilding other index
  • bengals preseason schedule 2011
  • carwash
  • mtv rivals
  • bengals history
  • randy moss legal issues
  • zara phillips husband
  • vince young uncle rico
  • vince young dadvince young eagles
  • chad ochocinco and cheryl burke
  • trojans
  • search 50 cent
  • search engines other than google
  • chad ochocinco to detroit
  • la ink ink
  • textured
  • battleship aurora
  • tims
  • glaze
  • northfield
  • vince young released
  • canary
  • la ink 105
  • c span yesterdayc span zelaya
  • new england patriots espn blog
  • speak
  • new england patriots 3 4
  • zara phillips wedding date
  • fenwick
  • new england patriots gillette stadium
  • chicago bears 17 lisa lampanelli
  • federated
  • battleship ipad
  • infrared
  • comforters
  • tea party manifesto
  • chicago bears expo 2011
  • chad ochocinco quotes video
  • steering
  • bea per capita income
  • bengals arrests
  • disassembledis boards
  • nicu
  • dis quand reviendras-tu
  • dis tester
  • chad ochocinco ultimate catch cast
  • stronger
  • bengals undraftedbengals vs steelers
  • cspan ap government review
  • connecticut renaissance faire
  • powertrain
  • bengals hard knocks episode 1
  • hp support error 1005
  • c span youtube obama
  • hp support 6310hp support 7200
  • towns
  • tint
  • cspan facebook
  • la ink 3rd season
  • hangers
  • chicago bears gifts
  • vince young 10 11
  • digitizing
  • connecticut food bank
  • mtv executivesmtv fantasy factory
  • hp support contact number
  • tea party hobbits
  • greg olsen puzzles
  • bea 2011 map
  • chad ochocinco xpchad ochocinco youtube
  • bengals qb situation
  • randy moss college
  • c span shelby foote
  • pudding
  • zara phillips and the queen
  • rufus
  • tea party chicago
  • hp support chat
  • bea goldfishberg
  • randy moss 98 vikings
  • zara phillips school
  • dis pater
  • tea party 8 28 09
  • michelin
  • tea party medicare
  • sticky
  • chicago bears tattoos
  • chicago bears 08 record
  • vince young endorsementsvince young foundation
  • cspan presidents
  • battleship kirishima
  • search and seizure
  • zara phillips and the queen
  • mtv youtube channel
  • search 78search 800 numbers
  • battleship yamato wreck
  • mtv live
  • la ink phone number
  • tea party zombies download
  • beagle
  • battleship lexington
  • bengals merchandise
  • battleship excel
  • dist 95
  • greensboro
  • search xml file
  • dailymotion
  • bea fox
  • bea rims
  • hp support chat
  • bengals cats for sale
  • hp support id
  • bengals new uniforms 2012
  • polos
  • cspan washington correspondents dinner 2011
  • bridesmaid
  • vince young injury
  • xanadu bengals
  • randy moss korey stringer
  • chad ochocinco vs skip bayless
  • opera
  • bea 0b0 105
  • vince young yahoo stats
  • bengals insider
  • squadron
  • dis poem
  • vince young jay cutler
  • hp support 6500a plus
  • mtv 5 cover
  • nozzle
  • bea 4603
  • bengals forum
  • bengals football
  • hp support 530
  • battleship bismarck wreck
  • freida pinto jeansfreida pinto kissing
  • hp support hard drive replacement
  • mtv oddities
  • scored
  • tea party ribbons
  • christina
  • birthdate
  • vince young 6
  • mtv 90s music videos
  • zara phillips tongue
  • search vim
  • bea test
  • connecticut lottery
  • freida pinto 1995
  • randy moss vikings 2011
  • dis 2012 conference
  • vince young status
  • connecticut quarter error
  • dis boards cruise
  • connecticut state parks
  • new england patriots 07
  • search 5500
  • vince young z
  • search tumblr
  • vince young rumors
  • c span yesterdayc span zelaya
  • connecticut 97.7connecticut attorney general
  • bea oracle
  • randy moss bio
  • freida pinto boyfriend