Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What does s~ do in Perl?

A Perl file I’m using contains many instances of s~. I’m not familiar with Perl and am having a hard time finding info on it. I know it’s related to a search and replace string. What does s~ do exactly?

Example:

s~ (Text)~ <a href="../images/text.pdf" target="_top">$1</a>~ig if /<p/;

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

While / is the most common delimiter, s uses whatever character immediately follows to set the delimiter to use in the rest of the expression. (This is true of many other operators, such as m, q, etc., as well.)

This example says to replace the text (Text) with an HTML anchor element, using the i and g flags.

The reason to use ~ rather than the conventional / is to avoid the need to escape the / in the replacement text, like

s/ (Text)/ <a href="..\/images\/text.pdf" target="_top">$1<\/a>/ig if /<p/;

More details can be found by running perldoc -f s.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading