Our Sponsors



Download BioinformaticsOnline(BOL) Apps in your chrome browser.




Perl subroutine to read and write files

  • Public
By Neelam Jha 2850 days ago
# Input output (InOut) the file # usage: # @array = InOut('read',$file) # $string = InOut('read',$file) # InOut('write',$file,\$string) # InOut('write',$file,\@array) #$string = "YO!"; #InOut('write','file.txt',\$string); sub InOut { my($bit,$file,$data) = @_; if($bit eq 'read'){ open InOut,"< $file" or die "Cannot open $file for input: $!\n"; my @file = <InOut>; close InOut; return wantarray ? @file : join '', @file; } if($bit eq 'write'){ open InOut,"> $file" or die "Cannot open $file for output: $!\n"; print InOut ref $data eq 'ARRAY' ? @$data : ref $data eq "SCALAR"? $$data : ''; close InOut; } }