On 20 July 2010 04:40, Mimi Cafe <mimic...@googlemail.com> wrote:
> I passed a has reference to my sub and tried dereferencing it within the sub
> and get an exception Can't use string ("1") as HASH ref while strict refs in
> use.
>
>
> my %mail_parameters = (
>        'Fname' => "$nickname_db_exist[1]",
>        'Lname' => "$nickname_db_exist[2]",
>        'E-mail' => "$reset_email",
>        'Subject' => "Your $site_name Password Reset",
>
>    );
>
>    # Send the requested information.
>    Mail_worker(\%mail_parameters);
>
>
> Sub mail_worker {
>
>    $received_arg = @_;
>    %params = %$received_arg;# Can't use string 1 as HASH ref while strict
> refs in use
>    .........
>    ..............
>

I hope Uri will forgive me but I will expand a little.

 $received_arg = @_;

You want to shift the argument list but you are actually assigned the
argument array to a scalar value and that will render it's size (1).
You passed Mail_worker a reference to a hash so that hash is now a
single scalar value. Then in your subroutine (called mail_worker, you
dropped the case in your example) after assigning the length of the
argument list to $recieved_arg you attempt to deference it, causing
the error. You could have kept the hash as a reference

sub Mail_worker {
    my $recieved_arg = shift;
   ....
    print "User first name is $recieved_arg->{Fname}\n";
   .....
}

You could have kept it as a hash but that's passing by value and
considered slightly poor practice as your passing more data that you
need. One more option would be to use a named parameter list

Mail_worker(Fname => $nickname_db_exist[1]",
                  Lname => "$nickname_db_exist[2]",
                  'E-mail' => "$reset_email",
                  'Subject' => "Your $site_name Password Reset",
              );

sub Mail_worker {
    my %args = @_
    print "User first name is $recieved_arg{Fname}\n"
}

I hope that helps,
Dp.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to