经常使用CPAN中的模块,会发现有些函数只接受文件句柄作为参数,如BioPerl中的SeqIO->new()。可如何从字符串变量中输入到参数中呢?很直接的想法就是将字符串转变成文件句柄。当然了,用Perl的好处就是你想到的东西,别人已经想到并且写好代码了,我们只需要拿来用就可以了。
而这次用的模块IO::String是Perl自带的,不需另外安装了。示例代码如下:
#!/usr/bin/perl -w
use strict;
use IO::String;
my $string = "This is the first line.nthis is the 2nd line.nthis is the last line."; # 定义一个三行的字符串;
my $F = IO::String->new($string);
my $first = <$F>;
print $first; # 打印出字符串中的第一行;

