in reply to Re: Code factory
in thread Code factory

Can you take this a step further and show how I could print out the users name, assuming one of the 'user' columns is 'name'? I understand the closure returns a reference to a subroutine - sort of - so, *getUserByUserId is a typeglob(is that the right term?) containing a reference to a subroutine. I don't know how to take that and actually refer to the user's name. Help me understand please :)

Replies are listed 'Best First'.
Re: Re: Re: Code factory
by sauoq (Abbot) on Jul 07, 2003 at 20:24 UTC
    I don't know how to take that and actually refer to the user's name.

    That local *getUserByUserId = make_closure( 'users', 'user_id' ); simply gives a name to the sub ref returned by make_closure(). You can call it exactly as if you had the code sub getUserByUserId { . . . } instead. To get at the name, you only have to know that the function returns the results of a fetchrow_hashref(). So, something like this should do fine:

    my $user_data = getUserByUserId( $id ); my $user_name = $user_data->{name};

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Re: Re: Code factory
by dragonchild (Archbishop) on Jul 07, 2003 at 20:13 UTC
    It's now a function. Treat it as such. my %vals = getUserByUserId('foo');

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      my %vals = getUserByUserId('foo');

      It's a function, yes. But it returns a reference to a hash, not a list.

      -sauoq
      "My two cents aren't worth a dime.";