Hi Perl Monks,
I am a beginner in perl. I am interested in writing the results of a subroutine for data formatting to
a text file. The cmd screen shows correct results but copying the results from cmd screen
using the mouse is often difficult. So I want the results directly to a text file on desktop.
Is there any perl syntax for this? I have given the perl code (fm.pl), the correct results of cmd and
the incorrect text file Fmd.txt. below. How can I overcome this problem? Please suggest me
some reading materials for writing to a text file in perl.
#!/usr/bin/perl-w
$a="BATCATEATFATRAT";
$b=print_sequence($a,3);
print"\n Scalar b=$b\n";
$output="Fmd .txt";
unless (open(RESULT,">$output")){
print"Cannot open file\"$output\".\n\n";
exit;
}
print RESULT"\n Formatted Data: $b\n\n";
close(RESULT);
exit;
sub print_sequence {
my($sequence, $length) = @_;
for (my $pos = 0; $pos < length($sequence); $pos +=
$length ) {
print substr($sequence, $pos, $length), "\n";
}
}
I have got the correct 3-letter words in cmd.
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\X>cd desktop
C:\Users\X\Desktop>fm.pl
BAT
CAT
EAT
FAT
RAT
Scalar b=
C:\Users\X\Desktop><code>
<p> But the text file Fmd.txt does not show the words:</p>
<code>Formatted Data: