Perl教程
Perl控制语句
Perl高级

Perl print()和say()

Perl print() 和 say()

Perl print() 函数是 Perl 用户最常用的函数之一。它将打印一个字符串、一个数字、一个变量或任何它作为参数的东西。
Perl 打印语法如下:
print"";

Perl 打印示例

我们来看一个简单的 Perl Print() 函数示例。
#!/usr/bin/perl
print"Welcome to lidihuo!!";
print"this is our Perl tutorial!!";
输出:
Welcome to lidihuo!! this is our Perl tutorial!!
以上输出将两个句子打印在同一行中。要将它们打印在不同的行中,我们可以在打印函数中使用"\n"换行符。

Perl 打印示例与 '\n'

' \n' 字符用于打印新行。用在打印函数的末尾。
#!/usr/bin/perl
print"Welcome to lidihuo!!\n";
print"this is our Perl tutorial!!\n";
输出:
Welcome to lidihuo!! 
this is our Perl tutorial!!

Perl 打印变量示例

要打印变量值,您需要在打印函数中传递变量。
#!/usr/bin/perl
$site = 'lidihuo';
print"Welcome to $site!!\n";
print"$site provides you all type of tutorials!!\n";
输出:
Welcome to lidihuo!!
lidihuo provides you all type of tutorials!!
这里,我们定义了一个变量$site。我们在字符串中传递了这个变量,它在输出中打印了它的值。

带单引号和双引号的 Perl 打印示例

Perl 打印函数不在内部插入单引号。这意味着它按原样打印单引号内的字符。它既不计算变量的值也不解释转义字符。
当上面的例子在单引号内运行时,你会注意到以下变化。
#!/usr/bin/perl
$site = 'lidihuo';
print'Welcome to $site!!\n';
print'$site provides you all type of tutorials!!\n';
输出:
Welcome to $site!! \n$site provides you all type of tutorials!!\n
在输出中,变量 $site 按原样打印。不评估其值。换行符也不会被解释。

Perl say()

较旧的 perl 版本不支持 say() 函数。它的作用类似于 print() 函数,唯一不同的是它会自动在末尾添加一个新行而不提及(\n)。
注意: 您需要在脚本中提及使用 say() 函数的版本。不使用版本,你会在输出中得到错误。
Perl say() 语法如下:
say "";

Perl say() 示例

我们来看一个简单的 perl say 函数示例。
#!/usr/bin/perl
use 5.010;
say "Welcome to lidihuo!!";
say "this is our Perl tutorial!!";
输出:
Welcome to lidihuo!! this is our Perl tutorial!! 
上面的输出在不使用换行符的情况下打印不同行中的两个句子。而这里我们使用的是use 5.010来支持say功能。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4