Format question

P

perlUSER

Hello;
I started learning Perl recently and am stuck with the Perl "format"
function.
----------------------------------------------------------------
Program:
#!/usr/bin/perl
use lib '/tmp/perl';
use FileHandle;

format STDOUT =
Album=@<<<<<<<<<<<<< Artist=@>>>>>>>>>>>> Price=$@##.##
$album, $artist, $price
..

$album = "Test album";
$artist = "Brit";
$price = "9.99";
write();
================================================================
Output:
$ perl rpt.pl

Album= Artist= Price=$ 0.00
$album, $artist, $price

$
===============================================================
Variables are printed as if they are like static values.

Could you please help me with this?

Thanks in advance.

Regards,
Sri.
 
J

John Bokma

perlUSER said:
Hello;
I started learning Perl recently and am stuck with the Perl "format"
function.
----------------------------------------------------------------
Program:
#!/usr/bin/perl
use lib '/tmp/perl';
use FileHandle;

format STDOUT =
Album=@<<<<<<<<<<<<< Artist=@>>>>>>>>>>>> Price=$@##.##
$album, $artist, $price
.

$album = "Test album";
$artist = "Brit";
$price = "9.99";
write();
================================================================
Output:
$ perl rpt.pl

Album= Artist= Price=$ 0.00
$album, $artist, $price

$
===============================================================
Variables are printed as if they are like static values.

perl form.pl
Album=Test album Artist= Brit Price=$ 9.99

ActiveState Perl @ XP.
 
P

perlUSER

Jim,
Thank you for sharing your thoughts. What is the equivalent function
for format or what do you recommend to a learner.

Regards,
Srini.
 
J

John Bokma

perlUSER said:
John,
Could you please let me know what I am missing?

I really don't know. I haven't used formats for ages, just copied your
example, and it works here. I recommend however to add:

use strict;
use warnings;

There are people who don't look at your code without those two, often for
very good reasons.

As Jim already suggested, have a look at perldoc perlform.
 
P

Paul Lalli

[Please quote some context when you are replying. Thank you.]
Thank you for sharing your thoughts. What is the equivalent function
for format or what do you recommend to a learner.

I've never really understood what formats can do that a simple
sprintf() cannot, personally.

Using your example:

#!/usr/bin/perl
use strict;
use warnings;

my $album = "Test album";
my $artist = "Brit";
my $price = "9.99";
printf "Album=%-13s Artist=%12s Price=\$%05.2f\n",
$album, $artist, $price;

__END__
Album=Test album Artist= Brit Price=$09.99

for more information,
perldoc -f sprintf

Hope this helps,
Paul Lalli
 
C

ced

Paul said:
I've never really understood what formats can do that a simple
sprintf() cannot, personally.

I don't recall if sprintf() is as full featured, but formats did
provide
a good visual preview of how the output will be rendered on the page.
Right/left justification and padding tokens were easy to remember and
usually more intuitive than with sprintf.
 
D

Darren Dunham

perlUSER said:
format STDOUT =
Album=@<<<<<<<<<<<<< Artist=@>>>>>>>>>>>> Price=$@##.##
$album, $artist, $price
.
$ perl rpt.pl
Album= Artist= Price=$ 0.00
$album, $artist, $price

There must be two separate lines under the 'format =' bit. If instead
you have one long line that wraps around on the screen, you would get
that output.

Just cutting and pasting your script into my machine gave me the
expected output. When I joined the two format lines together into one,
I got your output instead.
 
D

Darren Dunham

Paul Lalli wrote:
I don't recall if sprintf() is as full featured, but formats did
provide
a good visual preview of how the output will be rendered on the page.
Right/left justification and padding tokens were easy to remember and
usually more intuitive than with sprintf.

Also, it has some basic page support for standard page lengths. Most
output formats probably don't stick to 66 lines per page any longer, but
I used to use that to good effect... :)
 
G

George

perlUSER said:
Hello;
I started learning Perl recently and am stuck with the Perl "format"
function.
----------------------------------------------------------------
Program:
#!/usr/bin/perl
use lib '/tmp/perl';
use FileHandle;

format STDOUT =
Album=@<<<<<<<<<<<<< Artist=@>>>>>>>>>>>> Price=$@##.##
$album, $artist, $price
.

$album = "Test album";
$artist = "Brit";
$price = "9.99";
write();
================================================================
Output:
$ perl rpt.pl

Album= Artist= Price=$ 0.00
$album, $artist, $price

$
===============================================================
Variables are printed as if they are like static values.

Could you please help me with this?

Thanks in advance.

Regards,
Sri.

Though you code works like Bokma says,
but any way try this

use warnings;
use strict;
my ($album,$artist,$price);
format PRINT =
Album=@<<<<<<<<<<<<<, Artist=@<<<<<, Price=$@##.##
$album, $artist, $price
..

$album = "Test album";
$artist = "Brit";
$price = "9.99";
$~="PRINT";
write();

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Writint to a file 5
format/write question 2
format bug in vmsperl 0
format bug in vmsperl 2
ANN: Sequel 3.1.0 Released 0
ANN: Sequel 3.6.0 Released 0
format and newline 1
First Commercial Perl Program 30

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top