如何在perl的txt文件中打印输出?

时间:2014-02-28 06:31:04

标签: perl path adjacency-list

我有以下代码,我希望将输出导出并打印在新的自动创建的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);
    }
  }
}

1 个答案:

答案 0 :(得分:0)

#!/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)