[转 - 改] Perl初级教程 (三)

[转 - 改] Perl初级教程 (三),第1张

概述【文件处理】 下面是一个简单的perl程序,与UNIX中cat命令对某个文件的 *** 作相同。 #!/usr/local/bin/perl## Program to open the password file, read it in,# print it, and close it again. $file = '/export/home/dwei/ProgramScriptFol

【文件处理】

下面是一个简单的perl程序,与UNIX中cat命令对某个文件的 *** 作相同。

#!/usr/local/bin/perl## Program to open the password file,read it in,# print it,and close it again.
$file = '/export/home/DWei/ProgramScriptFolder/Textfile'; # name the fileopen(INFO,$file); # Open the file
#open(INFO,'/export/home/DWei/ProgramScriptFolder/Textfile'); # open函数打开一个文件并进行读 *** 作。第一个参数filehandle是指向文件的句柄。第二个参数为被打开的文件的文件名
@lines = <INFO>;		# Read it into an arrayclose(INFO);			# Close the fileprint @lines;			# Print the array
>

Introducing IDeaX Power Sessions
Here is your opportunity to engage in hyper-focused IDea-generating sessions to improve Advent’s business! Anthony Sperling is hosting the first session,with two topics:  
First,since referrals from our satisfIEd clIEnts are a huge source of our new sales,what are some IDeas of how we Could positively tap into our user community in ways that we are not today?

Secondly,APX has been a big market success for Advent and we still have a very large,opportunity to upgrade our remaining Axys customer base. What are some additional thoughts on how we Could encourage more and more Axys clIEnts to move up to our APX solution?

CategorIEs have been created for these topics. Using a category topic,post your IDeas or Vote and comment on other IDeas while the session is active (April 29-May 21). Anthony and his team will then revIEw the IDeas and share back with Advent how and when some of the IDeas will be put into action. We’ll recognize contributors and adopters.


#open语句也可以对文件进行输出和附加 *** 作。可以在文件名前加>进行输出 *** 作,用>>进行附加 *** 作:

 

 

open(INFO,">$file"); # Open for output

> [返回空,文件内容也清空]

 


open(INFO,">>$file");      # Open for appending
print INFO "Hello World!";   # input the text to file. 
close(INFO);

open(INfile,"$file");
@lines = <INfile>;
close(INfile);
print @lines;

 

> Hello World ! Hello World !

 


 

 【控制结构】
foreach
      

代码 @FruitList   =  ( @H_419_114@" @H_419_114@Apple  @H_419_114@" , @H_419_114@" @H_419_114@Pear  @H_419_114@" , @H_419_114@" @H_419_114@Orange  @H_419_114@" , @H_419_114@" @H_419_114@Peach  @H_419_114@" );
print ( @H_419_114@" @H_419_114@\n @H_419_114@" );
print ( @FruitList );
print ( @H_419_114@" @H_419_114@\n @H_419_114@" );
foreach   $Item  ( @FruitList )
{
    
print ( @H_419_114@" @H_419_114@\n @H_419_114@" );
    
print ( $Item );
}
>  Apple
  Pear
  Orange
  Peach
   

 for

 

 

 每次的 *** 作过程被包围在花括号内。程序块中的$Item第一次被赋予@FruitList数组中的第一个值,然后被赋予数组的第二个值,依次类推。如果@FruitList是空的,那么程序块将不会被执行

 

【判断】

判断是检验表达式结果是真是假的一种结构。在Perl中,任何非0数字和非空字符串被看作真。数字0、0字符串和空字符串被看作假。下面是一些基于数字和字符串的判断

 

$NumberA   =   1 ;
$NUmberB   =   2 ;
if  ( $NumberA   !=   $NumberB )  # == Is $a numerically equal to $b? {
    
print ( @H_419_114@" @H_419_114@Hello World! @H_419_114@" );
}
>  Hello World !  

$StrA = "A";
$StrB = "B";

if ($StrA eq $StrB)     # eq :Is $a string-equal to $b?
{
 print("Hello World!");
}              # ne : Is $a string-equal to $b?
else
{
 print("Here!");
}

> Here!  

 

 【逻辑】

 

$StrA   =   @H_419_114@" @H_419_114@A @H_419_114@" ;
$StrB   =   @H_419_114@" @H_419_114@0 @H_419_114@" ;

if  ( $StrA   &&   $StrB )
{
    
print ( @H_419_114@" @H_419_114@Hello World! @H_419_114@" );
}
else
{
    
print ( @H_419_114@" @H_419_114@Here! @H_419_114@" );
}
 
>  Here !  

$StrA = "A";
$StrB = "0";

if ($StrA || $StrB)
{
 print("Hello World!");
}
else
{
 print("Here!");
}

 > Hello World!  

$StrA = "A";
$StrB = "0";

if (! $StrB)
{
 print("Hello World!");
}
else
{
 print("Here!");
}

> Hello World!

总结

以上是内存溢出为你收集整理的[转 - 改] Perl初级教程 (三)全部内容,希望文章能够帮你解决[转 - 改] Perl初级教程 (三)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1282569.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存