Six Pakistan batsmen made ducks as Perera single-handedly won the match for the hosts in a single over - during which he claimed his hat-trick before running out Sohail Tanvir.
After Kumar Sangakkara fell three runs short of a 14th ODI century in anchoring the hosts' 243 for eight, Pakistan looked set to reel in the target as they reached 166 for two in reply. But the tourists' batsmen simply folded thereafter, at one stage losing seven for 13, to be bowled out for 199 with five overs unused.
Perera, fresh from his career-best six for 44 in the second match of the series, turned matters irretrievably his side's way when he removed Younus Khan, Shahid Afridi and then Sarfraz Ahmed, caught by Mahela Jayawardene at slip, from consecutive balls of the 41st over.
The 23-year-old was not done there though, as he acrobatically ran out Tanvir two balls later before ending the match by removing last man Saeed Ajmal to finish with four for 42. It was the 15th time in 18 chases above 240 that Pakistan have now come up short and left them 2-1 down in the series with a game to play.
Rain had washed out Wednesday's third game in Colombo and it threatened to interfere again on Saturday as the players were forced off for an hour with Sri Lanka 91 for three in the 26th over.
When the players returned Pakistan were made to pay after Umar Gul spilled a simple chance off Sangakkara, then on 35, who lofted Shahid Afridi straight at him. The left-hander cashed in on the life, and after reaching his 71st ODI half-century from 99 balls he upped his rate in a crucial 110-run stand with Jayawardene.
Their partnership was broken when Jayawardene (40) was bowled by Tanvir and an over later Sangakkara was judged caught in the deep going for his century.
The wicketkeeper-batsman had hit seven fours and three sixes in his 130-ball stay, most of which came after reaching 50, and his exit denied Sri Lanka from any significant late hitting.
That looked like it might be costly when Pakistan opener Azhar Ali (81 not out) and Misbah-ul-Haq (53) put on 113 to seemingly set Pakistan on course for a comfortable win. But when Misbah was expertly caught by Nuwan Kulasekera off Lasith Malinga in the deep Pakistan disintegrated in the face of Perera again.