De Villiers hit six boundaries in his 58-ball 51 and also shared 62 runs with Quinton de Kock (36) to take South Africa to 206-6 at tea.
The Proteas, who made 455-9 declared in their first innings before bowling out the hosts for 292, were sitting pretty with an overall lead of 369 runs with four wickets in hand.
No team has ever scored more than 300 runs in the final innings at Galle.
To their credit, the Sri Lankan spinners exploited well the dry and dusty track, accounting for all the wickets that fell in the South African second innings.
Perera struck early into the second session, claiming the big wicket of Hashim Amla, who is leading South Africa for the first time in Test cricket.
Amla (22) mistimed his flick and lobbed the ball to a diving Upul Tharanga, capping a disappointing show with the bat in the game. He had made 11 in his first innings.
Faf du Plessis (37) fell to a turner from Herath that crashed on to his stumps, dislodging the bails.
De Villiers frustrated the spinners with his shot-making until his stumps were shattered by Perera close to the tea break.
Earlier, South Africa started off cautiously against a bowling attack that relied heavily on spin in the absence of injured frontline seamer Shaminda Eranga.
Opener Dean Elgar, who made 103 in the first innings, was the first batsman to fall as he tried to go for an ambitious drive off Herath but nicked it behind to wicketkeeper Dinesh Chandimal.
Alviro Petersen (32) was given caught behind off Perera but the batsman asked for a review. TV replays remained inconclusive and in the end the decision of on-field umpire was upheld. The batsman walked off the field, shaking his head in disappointment.
The second and final Test will be played in Colombo from July 24-28.
You’ve reached your limit of {{free_limit}} free articles this month.
Subscribe now for unlimited access.
Already subscribed? Log in
Subscribe to read the full story →
Smart Quarterly
₹900
3 Months
₹300/Month
Smart Essential
₹2,700
1 Year
₹225/Month
Super Saver
₹3,900
2 Years
₹162/Month
Renews automatically, cancel anytime
Here’s what’s included in our digital subscription plans
Exclusive premium stories online
Over 30 premium stories daily, handpicked by our editors


Complimentary Access to The New York Times
News, Games, Cooking, Audio, Wirecutter & The Athletic
Business Standard Epaper
Digital replica of our daily newspaper — with options to read, save, and share


Curated Newsletters
Insights on markets, finance, politics, tech, and more delivered to your inbox
Market Analysis & Investment Insights
In-depth market analysis & insights with access to The Smart Investor


Archives
Repository of articles and publications dating back to 1997
Ad-free Reading
Uninterrupted reading experience with no advertisements


Seamless Access Across All Devices
Access Business Standard across devices — mobile, tablet, or PC, via web or app
