[英]How to print the output in a txt file in perl?
我有以下代碼,我希望將其輸出導出並打印在新的自動創建的txt文件中。
sub track {
my @path=@_;
my $last=$path[-1];
for my $next (@{$graph{$last}}) {
next if $next ~~ @path;
#next if grep {$_ eq $next } @path;
if ($next eq $stop){
print "\n";
print join ("->",@path,$stop),"\n";
} else {
track(@path,$next);
}
}
}
#!/usr/bin/perl
use strict;
use warnings;
use Path::Class;
use autodie; # die if problem reading or writing a file
sub writetofile(@mylist)
{
my $dir = dir("/tmp"); # /tmp
my $file = $dir->file("file.txt"); # /tmp/file.txt
# Get a file_handle (IO::File object) you can write to
my $file_handle = $file->openw();
foreach my $line ( @list ) {
# Add the line to the file
$file_handle->print($line . "\n");
}
}
call the above function after you have your output:
my @list = ();//here you have to insert all the output you have a print in the file
writetofile(@list)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.