#-- the sub that computes the data (@result) only from it's parameters (@pars) # Using @result as an array is just for simplicity. It could be hash-ref or an object # or whatever your expected result fits into... # sub get_result { my (@pars) = @_; ..... return @result; } #-- here's your CGI handler # sub cgi_handler { ... my (@pars) = ... #-- get params from HTTP request my (@result) = get_result( @pars ); ... #-- render HTTP-response from your @result } #-- somewhere else in your code... where your original question came from... # Instead of a regular function, an object method or a class method or an AUTOSUB etc. # could be called. # sub somewhere_else { #-- no worry to call cgi_handler(), just call @result = get_result( @pars ); ... }