CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
abourt format printing text

 



gshock
New User

Mar 1, 2009, 7:04 PM

Post #1 of 6 (1013 views)
abourt format printing text Can't Post

Hi,guys:

I am new to perl,and I am just following an example from the textbook.

use strict;
use warnings;

print "test format print:\n";
display();

sub display()
{
my $greeting='Hello';
my $planet='world';
write();

$greeting='Greetings';
$planet='Mars';
write();
}
format STDOUT=
I would like to say @<<<<<<<<<@<<<<<<<!
$greeting,$planet
.

but it turns out

"

main::display() called too early to check prototype at format_print.pl line 5.
Global symbol "$greeting" requires explicit package name at format_print.pl line
20.
Global symbol "$planet" requires explicit package name at format_print.pl line 2
0.
Execution of format_print.pl aborted due to compilation errors.

"

I am not sure what the problem is, so can you offer me some suggestions?


KevinR
Veteran


Mar 1, 2009, 8:08 PM

Post #2 of 6 (1011 views)
Re: [gshock] abourt format printing text [In reply to] Can't Post

Please lets us know the title of the book so we can avoid it.
-------------------------------------------------


FishMonger
Veteran / Moderator

Mar 1, 2009, 8:35 PM

Post #3 of 6 (1010 views)
Re: [gshock] abourt format printing text [In reply to] Can't Post

Throw that book away, it's not worth the paper it's printed on.

Get this book:
Learning Perl, Fifth Edition
http://oreilly.com/catalog/9780596520106/


gshock
New User

Mar 1, 2009, 9:06 PM

Post #4 of 6 (1007 views)
Re: [FishMonger] abourt format printing text [In reply to] Can't Post

Oh,guys,I am really sorry,but the code is not the original one,(the book supposed to be a good one)I just try to use subroutine to display the information in certain format.But I have no idea whether it works. the original code is:

use strict;
use warnings;

my $greeting='Hello';
my $planet='world';
write();

$greeting='Greetings';
$planet='Mars';
write();

...

format STDOUT=
I would like to say @<<<<<<<<<@<<<<<<<!
$greeting,$planet
.


(This post was edited by gshock on Mar 1, 2009, 9:14 PM)


FishMonger
Veteran / Moderator

Mar 2, 2009, 7:55 AM

Post #5 of 6 (1002 views)
Re: [gshock] abourt format printing text [In reply to] Can't Post

Variables used in a format statement need to be declared in the same scope as the declaration of the format. So, you need to either put the format statement in the sub, or declare the vars outside of the sub.

Also, you should remove the prototype and the default filehandle for format is STDOUT.


Code
use strict; 
use warnings;

print "test format print:\n";
display();

sub display
{
my $greeting = 'Hello';
my $planet = 'world';
write;

$greeting = 'Greetings';
$planet = 'Mars';
write;

format =
I would like to say @<<<<<<<<<@<<<<<<<!
$greeting,$planet
.

}


or


Code
use strict; 
use warnings;

my ($greeting, $planet);
print "test format print:\n";
display();

sub display
{
$greeting = 'Hello';
$planet = 'world';
write;

$greeting = 'Greetings';
$planet = 'Mars';
write;
}

format =
I would like to say @<<<<<<<<<@<<<<<<<!
$greeting,$planet
.


or


Code
use strict; 
use warnings;

print "test format print:\n";
display('Hello', 'World');

sub display
{
my ($greeting, $planet) = @_;
write;

$greeting = 'Greetings';
$planet = 'Mars';
write;

format =
I would like to say @<<<<<<<<<@<<<<<<<!
$greeting,$planet
.

}



gshock
New User

Mar 2, 2009, 3:05 PM

Post #6 of 6 (996 views)
Re: [FishMonger] abourt format printing text [In reply to] Can't Post

Oh,get it,but it appears the problem "syntax error at netformat.pl line 15, near "format =""when running.
Still sometime, when I use the format control,there are promotions that

" Format not terminated at exact.pl line XX, at end of line

syntax error at exact.pl line XX, at EOF",

I wonder how it happens.


(This post was edited by gshock on Mar 2, 2009, 3:21 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives