现在有点不清楚从哪里下手学了,难怪有人认为perl一团糟,预定义的东西、语法太多了,而且很多都有一定的关系。不过仍相信它功能强大。
一、文件件操作
可以使用open来打开,如果普通变量从filehandle读取,则取得文件的首行,如果将内容读入一个@,则每行为数组中的值。
打开文件:
open(INFILE, "input.txt") or die "Can't open input.txt: $!"; open(OUTFILE, ">output.txt") or die "Can't open output.txt: $!"; open(LOGFILE, ">>my.log") or die "Can't open logfile: $!"; |
赋值:
my $line = ; my @lines = ; |
读取:
filehandle本身也可以作为一个数组来读取,如下
while () { # assigns each line in turn to $_ print "Just read in this line: $_"; } 效果等同 while (@lines){ print "Just read in this line: $_"; } |
关闭:打开一个文件有必要将其关闭掉,如果不关,
close INFILE;
二、USE使用
1、用法:use Module VERSION LIST (D:\manual\perl\perldoc-html\functions\use.html)
use Module VERSION or use Module LIST or use Module or use VERSION |
Is equivelant to
BEGIN { require Module; import Module LIST; } |
2、export:一定要慎用export
pay attention:
1/ Do not export method names!
2/ Do not export anything else by default without a good reason!
*Exports pollute the namespace of the module user. 如果必需export操作,最好用@EXPORT_OK代替@EXPORT,并且避免使用短的或常用的符号以防命名冲突。
* 事实上不export仍然可以访问module 中的内容,使用方法ModuleName::item_name (or $blessed_ref->method)
3、import: import用法见use
ModuleName;
This imports all the symbols from ModuleName's @EXPORT into the namespace of the
statement.
ModuleName ();
This causes perl to load your module but does not import any symbols.
ModuleName qw(...);
[从前面的export看来,这类import方法最可取,不明白]
This imports only the symbols listed by the caller into their namespace. All listed symbols must be in your @EXPORT or @EXPORT_OK, else an error occurs. The advanced export features of Exporter are accessed like this, but with list entries that are syntactically distinct from symbol names.
三、Run & DEBUG
1/ 在执行前可以检查一下语法
perl -c filename
2/ DEBUG
perl -d filename
DEBUG使用帮助
v:可看出当前执行到哪一句,可以看到=>标识
l(el) line:切换到哪一行去执行
.(dot): show next 执行
c/n. 下一行
b. break
阅读(1351) | 评论(0) | 转发(0) |