设为首页 收藏本站
查看: 1068|回复: 0

[经验分享] perl学习笔记(6)

[复制链接]

尚未签到

发表于 2018-8-31 11:52:53 | 显示全部楼层 |阅读模式
  学习文件操作。
  句柄名字最好使用大写字母,这样就不会与保留字foreach,else,if等发生冲突。
  如果文件句柄名字重复使用,即另一个文件用相同的文件句柄名字打开,那么原始文件句柄将先被关闭,然后重新打开。
  打开文件时的异常处理:
  
$!在数字上下文中返回错误号,在字符串上下文中返回错误信息。
  
抛出异常信息时,die中断程序,warn不中断程序。
  
抛出异常信息时,带\n则不输出更多信息,否则要有at filename line XXX。
  练习(f61.pl):
  


  • $ cat f61.pl
  • #!/usr/bin/perl -w
  • open(F51,"XXXX.XX") or warn "Can not open file: $!";
  • open(F51,"XXXX,XX") or warn "Can not open file: $!\n";
  • open(F51,"abc.txt") or die "Can not open file: $!";
  • $contents = ;
  • #chomp $contents;
  • print qq(\n\$contents=$contents);
  • @contents = ;
  • #chomp @contents;
  • print qq(\n\@contents=@contents);
  • close(F51);
  • open(F51,"abc.txt") or die "Can not open file: $!";
  • print("\n use operator  read lines: \n");
  • while(){
  •         print(qq($_));
  • }
  • close(F51);
  

  $ perl f61.pl
  
Can not open file: No such file or directory at f61.pl line 2.
  
Can not open file: No such file or directory
  $contents=first line
  @contents=second line
  
third line
  use operator  read lines:
  
first line
  
second line
  
third line
  

  写文件和打开文件语法相似,不同的是路径前加>表示打开时先清空文件,>>表示从文件末尾添加。
  
练习(f62.pl):
  


  • #!/usr/bin/perl -w

  • open(F51,">efg.txt") or die $!;
  •         print F51 "line 1\n";
  •         print F51 "line 2\n";
  •         print F51 "line 3";
  • close(F51);

  • print("\nshow file:\n");
  • system("cat efg.txt");

  • open(F51,">>efg.txt") or die $!;
  •         print F51 "\nline 4";
  • close(F51);

  • print("\nshow file:\n");
  • system("cat efg.txt");

  • print("\n");
  

  $ perl f62.pl
  show file:
  
line 1
  
line 2
  
line 3
  
show file:
  
line 1
  
line 2
  
line 3
  
line 4
  

  当你将数据写入一个文本文件时, P e r l将\ n字符序列转换成你的操作系统使用的记录分隔符。在U N I X中,\ n变成一个ASCII 10(L F);在M a c i n t o s h上,\ n转换成ASCII 13(C R);在D O S和Wi n d o w s系统上,它变成序列ASCII 13和ASCII 10(C R L F)。
  每当你真的想要写入二进制数据并且不希望P e r l或操作系统对它进行转换时,你必须使用b i n m o d e函数,将文件句柄标记为二进制文件。应该在文件句柄打开之后和对它进行输入输出之前使用b i n m o d e。
  open(F51,"abc.gif") or die "$!";
  
binmode(F51);
  
#...
  
print F51 "...";
  
#...
  
close(F51);
  对文件进行检查:
  
                   -r  File is readable by effective uid/gid.
  
                   -w  File is writable by effective uid/gid.
  
                   -x  File is executable by effective uid/gid.
  
                   -o  File is owned by effective uid.
  -R  File is readable by real uid/gid.
  
                   -W  File is writable by real uid/gid.
  
                   -X  File is executable by real uid/gid.
  
                   -O  File is owned by real uid.
  -e  File exists.

  
                   -z  File has zero>
  
                   -s  File has nonzero>  -f  File is a plain file.
  
                   -d  File is a directory.
  
                   -l  File is a symbolic link.
  
                   -p  File is a named pipe (FIFO), or Filehandle is a pipe.
  
                   -S  File is a socket.
  
                   -b  File is a block special file.
  
                   -c  File is a character special file.
  
                   -t  Filehandle is opened to a tty.
  -u  File has setuid bit set.
  
                   -g  File has setgid bit set.
  
                   -T  File is an ASCII text file (heuristic guess).
  
                   -B  File is a "binary" file (opposite of -T).
  -M  Script start time minus file modification time, in days.
  
                   -A  Same for access time.
  
                   -C  Same for inode change time (Unix, may differ for other platforms)
  Example:
  while () {
  
                       chomp;
  
                       next unless -f $_;      # ignore specials
  
                       #...
  
                   }
  看到一句话也许有用:getc能够从文件读取单字符输入。
  继续学习:
  
不同的操作系统和不同的文件系统需要使用不同类型的锁定机制。
  
flock函数可以给UNIX和NT下的文件加锁。lock_SH共享锁,lock_EX专用锁。关闭文件自动释放锁,lock_UN释放锁很少需要这样做。
  
因为要先open后lock,这两个操作有空档,通过一个辅助文件可实现open前加锁。所有程序都先去锁信号文件,锁定信号文件成功后再open数据文件。
  

  练习(f63.pl):
  


  • #!/usr/bin/perl -w
  • use Fcntl qw(:flock);

  • my $lock_file1="/usr/tmp/lock1.lck";
  • sub get_lock1 {
  •         open(LOCK1,">$lock_file1") or die $!;
  •         flock(LOCK1,LOCK_EX) or die "Lock failed: $!";
  • }
  • sub release_lock1 {
  •         close(LOCK1);
  • }
  • print "press any key to get lock1:";
  • $WAIT=;
  • get_lock1();
  • print("\nget lock1,input your text to file efg.txt: ");
  • open(F1,">efg.txt") or die $!;
  • $WAIT=;
  • print F1 $WAIT;
  • close(F1);
  • print("\npress any key to release lock1 and exit:");
  • $WAIT=;
  • release_lock1();
  • print qq(\n);
  

  练习文件锁需要开两个窗口,下面是练习过程:
  

DSC0000.gif DSC0001.gif DSC0002.gif

  flock上锁不是操作系统级别的,只是程序之间的约定。例如:程序A用flock加锁,程序B不用flock仍然可以操作文件。flock用于A,B之间约定不要同时操作同一个文件。
  这次就到这。



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-559339-1-1.html 上篇帖子: perl学习笔记(5) 下篇帖子: perl学习笔记(7)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表