鑽石操作符 <> :從用戶指定的位置讀取輸入(一般用於讀取用戶的輸入文件)
注意:
1,<>會處理所有的輸入,直到所有輸入的結尾爲止
一般在一個程序中只有一個<>,若出現多次則可能發生錯誤。
2,調用參數@ARGV @ARGV 是一個裝着調用參數的數組
標準輸入輸出
1,讀取從鍵盤輸入的值 <STDIN>
注意 :<STDIN>只讀取鍵盤輸入的一行,到換行符結束 (用chomp去掉換行符)
若<STDIN>沒有賦值給變量,則默認存在$_中
#!usr/bin/perl -w
use strict;
my $name = "linda";
print "$name\n";
print "please input your name :";
chomp (my $name1 = <STDIN>);
print "your name is $name1\n";
2,輸出到標準輸出 print (一般默認爲屏幕輸出)
3,段落輸出:
print << "EOF"; #遇到“EOF”開頭的行才輸出結束 懷疑其實和shell一樣,不一定要是EOF這個字符,可以是其他。只要結束是一樣的即可
A :" welcome to ssdd,hehe~"
B:"what do you mean by 'hehe' ?"
EOF #EOF 開頭,輸出結束
printf :產生格式化的輸出結果,printf的參數包括兩部分:
1,用於填補空白的格式化字符;2,要輸出的數據列表
格式字符串中可以包含多個轉換,每一個轉換都以一個%符號開頭
printf "Hello ,%s; your password expires in %d days!\n",$user,$days;
文件輸入
open [fileHandle],[mode],[filePath];
#以只讀方式打開文件/home/user/text.txt
open IN,"</home/user/text.txt" or die $!; #$!表示輸出錯誤信息
close IN; #關閉文件
文件模式:
< 以只讀方式打開(默認)
> 以寫入方式打開,文件原有內容會被清空
>> 以追加寫的方式打開,寫入內容會增加到文件末尾
文件輸出
open [fileHandle],[mode],[filePath];
#以寫入方式打開文件/home/user/text.txt 綁定到OUT句柄
open OUT, '>',"/home/user/text.txt" or die $!;
open OUT,">/home/user/text.txt" or die $!;
#寫入內容到/home/user/text.txt中
print OUT "A test of output test.txt";
close OUT;#關閉文件
讀寫gz壓縮文件
my $gzFile = "luckystar.txt.gz";
open IN,"gzip -dc $gzFile|" or die $!; # | 管道符
do something~;
close IN;
open OUT,"| gzip -9> $gzFile" or die $!;
do something~;
close OUT;
逐行讀取文件
my $file ="/home/file.txt";
open IN ,"<$file" or die $!; #打開文件
while (my $line =<IN>){ #用鑽石操作符獲取IN文件中的行
chomp $line; #去掉換行符
do something~
}
close IN;
#!usr/bin/perl -w
use strict;
open IN,"< ../EP/GS08358/GS08358.vcf" or die $!;
open OUT,"> ./EP.txt" or die $!;
while (my $line = <IN>){
chomp $line;
print OUT "$line\n";
}
close OUT;
close IN;