Question

I've to use perl as a part of our internship, I've come across this piece of code and could not understand what this may mean.

$val->ReadSim($first_sim, \&DataProcessing); 

In the script, subroutine DataProcessing is defined, but could not find ReadSim. I tried searching in our infrastructure but was not able to. This was given to me to understand a week ago and I can't ask the guide without losing credits...

Please help...

Was it helpful?

Solution

What you're seeing is not a mere subroutine, but a method on some object called $val.

I take it you might see something on top of the program like this:

use Foo::Bar;   # Some Perl module

This Perl module will contain the method ReadSim. Somewhere in your code, you probably see something like this:

my $val = Foo::Bar->new;   # If the people who wrote this followed standards...

This defines $val as an object of Foo::Bar. If you look in package Foo::Bar, you'd see something like this:

#! Foo/Bar.pm

package Foo::Bar;
use strict;  # Because I'm an optimist
use warnings;

...


sub new {
    my $class = shift;
    ...
    my $self = {};
    ...
    bless $self, $class;
    ...
    return $self;   # May simply be bless {}, $class;
}

Then further down, you'll see:

sub ReadSim {
    my $self = shift;
    ...
}

The $self = {} is a reference to a Perl hash. This is how most objects are defined. That's pretty much all the constructor does. It defines a reference to something, then blesses it as that object type. Then, the methods are merely subroutines that take the defined object and manipulate that.

$val-> ReadSim(...);

is equivalent to:

Foo::Bar::ReadSim( $val, ... );

So much for your introduction to Object Oriented Perl by Fire. You still have a question about what does ReadSim mean.

If all is right in the world, the developer of that module should have created built in Perl documentation called POD. First, determine the type of object $val is. Look where $val is defined (Something like my $val = Foo::Bar->new(...);). The Foo::Bar is the class that $val is a member of. You can do this from the command line:

$ perldoc Foo::Bar

And, if you're lucky, you'll see the documentation for Foo::Bar printed out. If you're really, really lucky, you will also see the what ReadSim also does.

And, if you're not so lucky, you'll have to do some digging. You can do this:

$ perldoc -l Foo::Bar
/usr/perl/lib/perl5/5.12/Foo/Bar.pm

This will print out the location of where the Perl Module resides on your system. For example, in this case, the module's code is in /usr/perl/lib/perl5/5.12/Foo/Bar.pm. Now, you can use an editor on this file to read it, and search for sub ReadSim and find out what that subroutine ... I mean method does.


One final thing. If you're new to Perl, you may want to look at a few tutorials that come with Perl. One is the Perl Reference Tutorial. This tutorial will tell you about references. In standard Perl, there are three different types of variables: scalar, hashes, and arrays. To create more complex data structures, you can create hashes of hashes or hashes of arrays, or arrays of arrays, etc. This tutorial will teach you about how to do this.

Once you understand references, you should read the tutorial on Perl Object Oriented Programming. Object Oriented Perl uses references to create a simulated world object oriented programming design. (I say simulated because some people will argue that Object Oriented Perl isn't really object oriented since you don't have things like private methods and variables. To me, if you can think in terms of objects and methods as you program, it's object oriented).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top