print output the containt to STDIO.
- @fed = qw /test this that/;
-
print @fed;
-
print "@fred";
-
-
testthisthat
-
test this that
if the argument is a statement use the parentheses.
print ((2+3)*4);
the return value of print can be true or false. only when you print some I/O error it will be false.
printf format the print.
Generally, you won’t use an array as an argument to printf. That’s because an array
may hold any number of items, and a given format string will work with only a certain
fixed number of items: if there are three conversions in the format, there must be exactly
three items.
- my @items = qw( wilma dino pebbles );
-
printf "The items are:\n".("%10s\n" x @items), @items;
The items are:
wilma
dino
pebbles
- print "What column width would you like? ";
-
chomp(my $width = <STDIN>);
-
print "Enter some lines, then press Ctrl-D:\n"; # or Ctrl-Z
-
-
chomp(my @lines = <STDIN>);
-
print "1234567890" x (($width+9)/10), "\n"; # ruler line as needed
-
foreach (@lines) {
-
printf "%${width}s\n", $_;
-
}
%${width}s:the {}is used to insulate the variable width with 's'.
People who have seen printf before may have thought of another solution. Because
printf comes to us from C, which doesn’t have string interpolation, we can use
the same trick that C programmers use. If an asterisk (*) appears in place of a
numeric field width in a conversion, a value from the list of parameters will be used:
printf "%*s\n", $width, $_;
阅读(674) | 评论(0) | 转发(0) |