Escape Character in Perl example Script

 

Hello,

 

The script below explains on how to use escape characters in a perl environment

1. Perl will escape back slash “\” by default so if you referring a path to a windows system please make sure that you use two back slashes to refer one \

Eg: c:\\windows upon compilation will be referred as c:\windows

2. To preserve ” and “” from being executed we can use single “q” with braces like q{}, in other words q{} is similar to ” in perl enviroment where contents inside ” will not be executed and double “q” with braces like qq{} will execute the contents inside the braces just like “” where conents inside “” will be executed. The below script will explain you better.

#/usr/bin/perl -w
#Escape character for perl
print "c:\\windows ", 'This will print the string as c:\windows', "\n";
print q{This will preserve '' and "" without being compiled}, "\n";
print qq{This will compile contents between the braces\n};

The output of above perl script will be

c:\windows This will print the string as c:\windows
This will preserve '' and "" without being compiled
This will compile contents between the braces




Leave a Reply

Protected by WP Anti Spam