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

I'm still working to learn to use DBIx::UserDB.

My browser reports:

Software error: Can't use string ("wheel") as a HASH ref while "strict refs" in use at + /usr/local/share/perl/5.8.7/DBIx/UserDB.pm line 410.
Here is the debugging output I'm sending to the error log:

New UserDB object created. The $session ID is: 77540362YTIvQiAaOS7yA942A153i62p32351f159U25f59k22l181r1112198r113k146 +y85A21O160w104393P643515121S122n190n96A21067w174n66c98l100j95w174e13j +125g293123n96B60G9EPZdZYJ4zjW1. About to enter dispatch code. $function is NewGroup. [Thu Feb 2 22:29:32 2006] test-auth5.cgi: Odd number of elements in h +ash assignment at /var/wwwssl/auth-test/test-auth5.cgi line 379. GroupCreate received these arguments: $userdb is DBIx::UserDB=HASH(0x84febec). The new group name is: wheel. $group is HASH(0x84fecac). Creating a new group called: wheel. ref of group = ref(HASH(0x84fecac)) ref of groupname = ref(wheel) [Thu Feb 2 22:29:32 2006] test-auth5.cgi: Can't use string ("wheel") +as a HASH ref while "strict refs" in use at /usr/local/share/perl/5.8 +.7/DBIx/UserDB.pm line 410.
I'm using this syntax:

&GroupCreate($userdb,\%group);
to call a subroutine which includes line 379, the line which accepts @_ and parses it at the top of the following snippet.

sub GroupCreate() { my($userdb,%group) = @_; print STDERR "GroupCreate received these arguments: \n\t\$userdb is +$userdb. \n\tThe new group name is: $group->{groupname}.\n\t\$group +is $group.\n"; return p("Groupname required to create new group.") unless defined($ +group->{groupname}); my($status,$gid); print STDERR "Creating a new group called: $group->{groupname}.\n"; print STDERR "\tref of group = ref($group) \n\tref of groupname = re +f($groupname) \n"; { no strict 'refs'; my $result = $userdb->group_create ( $group->{groupname} ); } if($result) { $status = p("A New Group, called <b>$group{groupname}</b> has been + successfully created, with GID: $group->{'gid '}."); } else { $status = p("The creation of a new Group, called <b>$group{groupna +me}</b> failed."); } return $status; } # END GroupCreate
And line 410 which throws that other error is in this subroutine in a distinct module, on the line which defines $old_group.

sub group_create { my ( $self, $group ) = @_; my $DB = $self->{DB}; # Check for group with same name my $old_group = $DB->record_search( $self->{group_profile}, { groupname => $group->{groupname} } ); return undef if @$old_group; $DB->record_insert( $self->{group_profile}, $group ); my $new_group = $DB->record_search( $self->{group_profile}, { groupname => $group->{groupname} } ); die "Failed to find newly created group\n" unless @$new_group == 1 +; # Copy the fields of the new user back in this one while ( my ($name,$value) = each %{$new_group->[0]} ) { $group->{$name} = $value; } return $group; }
If this is too confusing to be helpful and you've successfully used DBIx::UserDB, I'd be happy for the moment simply having some working code snippets I could adapt from.

-- Hugh

Replies are listed 'Best First'.
Re: Confused Still by Hash Refs
by g0n (Priest) on Feb 03, 2006 at 12:06 UTC
    Your problem is this:

    my($userdb,%group) = @_;
    if you are calling it with this:

    &GroupCreate($userdb,\%group);

    You are sending the sub a scalar (I assume) and a hashref, but the assignment

    my($userdb,%group) = @_;
    is trying to assign a hash, not a hashref, and therefore expecting the parameters after $userdb to be a list (i.e. the contents of the hash itself). As an alternative, try:
    my ($userdb,$groupref) = @_; my %group = %$groupref;
    my ($userdb,$group) = @_;

    FWIW, thats what:

    [Thu Feb 2 22:29:32 2006] test-auth5.cgi: Odd number of elements in h +ash assignment at /var/wwwssl/auth-test/test-auth5.cgi line 379.

    is telling you.

    Update: all the uses of %group in the sub appear to be addressing a hashref $group, with the exception of the assignment, so there's no need to dereference it at all.

    --------------------------------------------------------------

    "If there is such a phenomenon as absolute evil, it consists in treating another human being as a thing."
    John Brunner, "The Shockwave Rider".

      Thank you, sir. That handled the error in test-auth5.cgi at line 379, the "odd number of elements in hash assignment" piece. It leaves me with the hash ref error in the module I'm trying to use, suggesting to me that I'm not sending DBIx::UserDB its arguments in exactly the right manner, somehow. Any further ideas? -- Hugh
        Yep, take a look at DBIx::Userdb. The syntax is:

        group_create(\%hash);

        and you're doing:

        $userdb->group_create ( $group->{groupname} )

        $group->{groupname} isn't a hashref, in fact AFAICT it is the value 'wheel'. Try this:

        $userdb->group_create($group);

        or

        $userdb->group_create({groupname=>$group->{groupname}});

        if you don't want to pass the rest of the contents of %$group in.

        --------------------------------------------------------------

        "If there is such a phenomenon as absolute evil, it consists in treating another human being as a thing."
        John Brunner, "The Shockwave Rider".

Re: Confused Still by Hash Refs
by hesco (Deacon) on Feb 03, 2006 at 07:42 UTC
    FYI, that groupname gets passed, using CGI, like so:
    my $groupname = $q->param('groupname') || ''; my $group = { groupname => $groupname };