kosta has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a perl module that has this function
sub passwd { my $cleartext_pass=$_[0]; my $ppr = Authen::Passphrase::MD5Crypt->new( salt_random => 1, passphrase => $cleartext_pass); my $passwdo = $ppr->as_crypt; return $passwdo; }
I use it in my scripts like this:
my $rela ... #This is the module that is "multi-level" and the DB part + has passwd function in it ... if (param('pass') == param ('pass_again')) { my $pass_clear=param('pass'); my $password=$rela->passwd($pass_clear); $rela->do('INSERT INTO uzivatele (jmeno,prijmeni,heslo,login,s +tav_uctu,skupina,vytvoren) VALUES (?, ?, ?, ?, 2, \'user\', CURRENT_T +IMESTAMP)', param('jmeno'), param('prijmeni'), $password, param('logi +n'));
Now the problem is when I run the function alone (not from the module) with a string like this:  my $test=passwd('hi');
it produces the right output (md5crypted password)
but when I use it from the module in my script (as shown above) I get incorrect md5crypt code... The thing is that I pass the cleartext to passwd function from param ... I tried storing just the cleartext param (I changed the passwd function to return just $cleartext_pass It stored something like :
My::Module = HASH(0x2ab7df04ba50) as password. So the problem is with passing it to the function from param. What am I doing wrong here?

Replies are listed 'Best First'.
Re: annoying HASH (0xsomething)
by Anonyrnous Monk (Hermit) on Jan 18, 2011 at 14:05 UTC

    When you call a sub as a method, such as $obj->passwd(...), the first parameter being passed to the method is a (blessed) reference to the object instance (often assigned to a variable $self within the method). This is what stringifies as My::Module = HASH(0x2ab7df04ba50) in your case.

    $rela->passwd($pass_clear); # passes two arguments: objref, pass +_clear My::Module::passwd($pass_clear); # passes one argument: pass_clear

    You should decide whether you want a method or a regular subroutine.  A method makes sense, if you need access to the object's instance data within the method — which doesn't seem to be the case with your passwd routine.

      Thank you very much dude!
      I am so much used to Java for OOP and Perl for just straight scripting. It is terrible when I try to mix my knowledge of both :D I solved my problem by adjusting the line with getting the parameter (just passed number 1 instead of 0)
      sub passwd{ >>$pass_cleartext=$_[1]; }

      thanks again and have a good one

        This will now fail if you call it as a subroutine. :) Try this instead:

        sub passwd { my $ppr = Authen::Passphrase::MD5Crypt->new( salt_random => 1, passphrase => pop); return $ppr->as_crypt; }

        Now it will work no matter which way you invoke it.

        -- 
        Education is not the filling of a pail, but the lighting of a fire.
         -- W. B. Yeats
Re: annoying HASH (0xsomething)
by chromatic (Archbishop) on Jan 18, 2011 at 19:03 UTC

    Beware!

    param('pass') == param ('pass_again')

    If your passwords are always numeric, that will work -- but your passwords probably contain characters. Use a string comparison to avoid numeric coercion:

    param('pass') eq param ('pass_again')