当前位置: 动力学知识库 > 问答 > 编程问答 >

Perl format doesnt use declared variables

问题描述:

I am new to perl and am trying to to format a file. the output only produces the format as it is type not with the scalar values. I am not sure what I'm missing. Thanks for helping.

use strict;

use warnings;

print "1\a";

my $a = <STDIN>;

print "2\a";

my $b = <STDIN>;

print "3\a";

my $c = <STDIN>;

open DB, ">/perl/folder/data.txt" or die ('1');

format DB =

%%%%%%%%%%%

$a

$b

$c

%%%%%%%%%%%

.

write DB;

close DB;

网友答案:

As Miller pointed out, don't use formats. It's a really bad practice.

See perldoc perlform foi how formats are specified. As pilcrow pointed out, you didn't specify any records.

Here is an example with three left aligned records with the fixed width of 6 characters.

use strict;
use warnings;

print "1\a";
my $a = <STDIN>;
print "2\a";
my $b = <STDIN>;
print "3\a";
my $c = <STDIN>;

my $out_fn = 'formated.txt';
open DB, '>', $out_fn or die "could not open $out_fn: $!";
format DB = 
%%%%%%%%%%%
@<<<<<<
$a
@<<<<<<
$b
@<<<<<<
$c
%%%%%%%%%%%
.

write DB;
close DB;
分享给朋友:
您可能感兴趣的文章:
随机阅读: