个性化阅读
专注于IT技术分析

Perl字符串转义字符

点击下载

本文概述

所有特殊字符或符号(如@, #, $, &/, \等)均无法正常打印。他们需要在前面的转义字符反斜杠(\)进行打印。


Perl显示电子邮件地址

所有电子邮件地址都包含(@)符号。如前所述, 符号通常不会在字符串内打印。他们需要特别注意。 @符号前使用反斜杠(\)来打印电子邮件地址。

use strict;
use warnings;
my $site  = "srcmini\@gmail.com";
print "$site\n";

输出

srcmini@gmail.com

Perl $符号嵌入双引号字符串

如果要在字符串中打印($)符号, 请在$符号前面使用反斜杠(\)。

use strict;
use warnings;
my $msg1 = 'Ana';
print "We have defined \$msg1 as $msg1\n";

输出

We have defined $msg1 as Ana

Perl转义转义字符

如果要在字符串中打印(\)符号, 请在\符号前面使用反斜杠(\)。

use strict;  
use warnings;  
print "The \\n is a new line chracter in programming languages.\n";

输出

Everyone has to follow this rule whether its a boy\girl

Perl转义双引号

如果要在字符串中打印双引号, 请在两个引号处都使用反斜杠(\)。

use strict;
use warnings;
my $x = 'tutorials';
print "Our site \"srcmini\" provides all type of \"$x\"\n";

输出

Our site "srcmini" provides all type of "tutorials?

Perl Double-q运算符q

” qq”运算符用括号将字符串周围的双引号替换。这意味着(“”)在此字符串上不再是必需的。它只会用qq打印字符串。

但是, 如果你想在字符串中使用方括号, 则需要在字符串周围使用大括号{}。

而且, 如果你需要在字符串内使用花括号, 请在字符串周围使用方括号[]。

#use of qq
use strict;
use warnings;
my $x = 'tutorials';
print qq(Our site "srcmini" provides all type of "$x"\n);
#use of {} brackets
print qq{Our site (srcmini) provides all type of "$x"\n};
#use of [] brackets
print qq[Our site (srcmini} provides all type of "$x"\n];

输出

Our site "srcmini" provides all type of "tutorials"
Our site (srcmini) provides all type of "tutorials"
Our site (srcmini} provides all type of "tutorial"

Perl单q运算符, q

单个” q”运算符用作字符串中的单引号(‘)。像单引号一样, 它也不插入变量。

#use of q
use strict;
use warnings;
my $x = 'tutorials';
print q(Our site "srcmini" provides all type of "$x"\n);
print"\n";
#use of () brackets
print q(Our site "srcmini" provides all type of "$x"\n);
print"\n";
#use of {} brackets
print q{Our site )srcmini( provides all type of "$x"\n};
print"\n";
#use of {} brackets
print q[Our site )srcmini} provides all type of "$x"\n];

输出

Our site "srcmini" provides all type of "$x"\n
Our site "srcmini" provides all type of "$x" \n
Our site )srcmini( provides all type of "$x" \n
Our site )srcmini} provides all type of "$x" \n
赞(0)
未经允许不得转载:srcmini » Perl字符串转义字符

评论 抢沙发

评论前必须登录!