convert php function to perl