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!

10
задан BrianH 8 April 2011 в 15:37
поделиться