usa ims
usa ims

Reputation: 145

Getting wrong results with Perl subroutine

I have a test file that looks like this:

my $int = new Services::Plugins::FTP::ftp;
$int->AddUser("durr");

The module has this code:

sub AddUser {

my( @username ) = @_;
print "@_\n";  

}

But my results looks like: Services::Plugins::FTP::ftp=HASH(0x2490160)durr

I just want 'durr'.

Why am I getting the extra stuff?

Upvotes: 1

Views: 106

Answers (3)

choroba
choroba

Reputation: 241758

You are using Object Oriented approach ($int->AddUser("durr") is a method invocation). When calling a method, the first argument is always the object or the class the method should be applied to.

Upvotes: 3

Quentin
Quentin

Reputation: 943108

The first argument to $foo->bar() is $foo

sub AddUser {
    my($self, @username ) = @_;
    print "@_\n";  
}

Upvotes: 5

user507077
user507077

Reputation:

You're dealing with object-oriented Perl. If you call a function on an object instance, as in your case with $instance->function(), then the very first parameter is the reference to the instance itself. It is most often called $self.

A commonly used idiom is to write instance methods like this:

sub some_method {
  my ($self, @args) = @_;
}

I suggest you read up on Perl's object-oriented system in the perlootut man page (a good tutorial).

Upvotes: 9

Related Questions