Thisara Perera made the most of an opportunity for a free hit, as Sri Lanka tried to recover from a batting collapse against New Zealand.