Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does this call to a subroutine in a Perl module work?

I recently saw some Perl code that confused me. I took out all of the extra parts to see how it was working, but I still don't understand why it works.

Basically, I created this dummy "module" (TTT.pm):

use strict;
use warnings;

package TTT;
sub new {
    my $class = shift;
    return bless {'Test' => 'Test'}, $class;
}

sub acquire {
    my $tt = new TTT();
    return $tt;
}
1;

Then I created this script to use the module (ttt.pl):

#!/usr/bin/perl
use strict;
use warnings;

use TTT;
our $VERSION = 1;

my $tt = acquire TTT;
print $tt->{Test};

The line that confuses me, that I thought would not work, is:

my $tt = acquire TTT;

I thought it would not work since the "acquire" sub was never exported. But it does work. I was confused by the "TTT" after the call to acquire, so I removed that, leaving the line like this:

my $tt = acquire;

And it complained of a bareword, like I thought it would. I added parens, like this:

my $tt = acquire();

And it complained that there wasn't a main::acquire, like I thought it would.

I'm used to the subroutines being available to the object, or subroutines being exported, but I've never seen a subroutine get called with the package name on the end. I don't even know how to search for this on Google.

So my question is, How does adding the package name after the subroutine call work? I've never seen anything like that before, and it probably isn't good practice, but can someone explain what Perl is doing?

Thanks!

like image 789
BrianH Avatar asked Apr 08 '11 15:04

BrianH


People also ask

How do you call a subroutine in Perl?

You can define a subroutine anywhere in your program. If you have subroutines defined in another file, you can load them in your program by using the use , do or require statement. A Perl subroutine can be generated at run-time by using the eval() function.

How do you call a function from a Perl module?

A module can be loaded by calling the use function. #!/usr/bin/perl use Foo; bar( "a" ); blat( "b" );

What is subroutine in Perl explain with suitable example?

A Perl function or subroutine is a group of statements that together perform a specific task. In every programming language user want to reuse the code. So the user puts the section of code in function or subroutine so that there will be no need to write code again and again.

How do I pass a variable to a subroutine in Perl?

Passing Arguments to a Subroutine When calling a subroutine, arguments can be passed to to it by writing them as a comma-delimited list inside the () . Inside the subroutine, these arguments are accessible using the special array @_ . The first argument to the function is in $_[0] , the second is in $_[1] , and so on.


2 Answers

You are using the indirect object syntax that Perl allows (but in modern code is discouraged). Basically, if a name is not predeclared, it can be placed in front of an object (or class name) separated with a space.

So the line acquire TTT actually means TTT->acquire. If you actually had a subroutine named acquire in scope, it would instead be interpreted as aquire(TTT) which is can lead to ambiguity (hence why it is discouraged).

You should also update the new TTT(); line in the method to read TTT->new;

like image 113
Eric Strom Avatar answered Oct 12 '22 03:10

Eric Strom


It's the indirect object syntax for method calls, which lets you put the method name before the object name.

As the documentation there shows, it's best avoided because it's unwieldy and it can break in unpredictable ways, for example if there is an imported or defined subroutine named acquire — but it used to be more common than it is today, and so you will find it pretty often in old code and docs.

like image 43
hobbs Avatar answered Oct 12 '22 02:10

hobbs