You're probably best having a script accept the username and password and then redirecting to the proper URL with the correct Basic authentication headers (filled out with the info passed via CGI). Otherwise, you'll have to set and check cookies.
If you want to stick with using Apache's htpasswd approach of managing users but don't mind setting/checking cookies throughout the site, then check out Apache::Htpasswd. It'll allow you to check a username/password pair from within a Perl script (such as one called from a form on a webpage).
There are plenty of other ways to authenticate users as well.