Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl difficulty passing values to function

Tags:

perl

Cannot understand why the returned values from the function login bellow do not correspond to what is passed to it.

The following is a snippet of my code

package This_package;
    .......   

    # returned from function that parses post data ($reqparam)
    my $thisuser = $$reqparam{"username"};

    # escape '@', username is an email
    $thisuser =~ s/@/\@/;
    my $thisuser_pass = $$reqparam{'password'};

    print $thisuser;      # ok
    print $thisuser_pass; # ok

    my $obj = new users;
    my $valid_user = $obj->login($thisuser, $thisuser_pass);
    .......

package Another_package;
    sub new {
        my ($class) = @_;
        my $self = {
            _login => undef,
            _create_user => undef,
            ....
            };
        bless $self, $class;
        return $self;
    }

    sub login ($$){
        my ($user, $pass) = @_;
        # some processing
        .....

       return $user;   # prints users=HASH(...)
       # return $pass; # prints the value of $user (the actual value)
                       # instead of the value of $pass
    }

While trying to learn perl by converting some code from php into perl. I have run into this problem, I have tried a few alternatives but obviously there is something I am not getting!

like image 340
jose antunes Avatar asked Jun 30 '26 03:06

jose antunes


2 Answers

When you call a function like

 my $valid_user = $obj->login($thisuser, $thisuser_pass);

The first parameter is this usually done as

sub login
{
    my ( $self , $user , $password ) = @_;
}

You are missing $self

Because you are missing $self you user is actually the object and your password is actually the user.

If you are coming from another objected oriented language like C++ , Java or C#, this is a perl gotcha (no pun intended :)) . Another one is that even from an object method if you want to invoke another member method you have to use self like

$self->callAnotherObject( $user );

Simply calling wont do

 callAnotherObject( $user );

Also I see that you are using function prototypes, It may not work as you intend it to be.

like image 156
parapura rajkumar Avatar answered Jul 02 '26 05:07

parapura rajkumar


When you use object-oriented syntax ($obj->login($thisuser, $thisuser_pass)) to call a subroutine, the first argument will be the object itself. You should say, and you will typically see object-oriented modules use syntax like:

sub login {
    my ($self, $user, $pass) = @_;
    ...
}

Incidentally, you shouldn't use prototypes ( ($$) ) without a good reason. Prototypes in Perl are not used in the same way they are in other languages, and in any case the prototype is ignored when you call a subroutine with indirect syntax (luckily, in your case, since you are actually calling it with 3 arguments).

like image 31
mob Avatar answered Jul 02 '26 04:07

mob



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!