
7stud
Enthusiast
Mar 31, 2010, 7:00 AM
Post #3 of 3
(225 views)
|
|
Re: [Nila] Why this simple script showing wired output
[In reply to]
|
Can't Post
|
|
This:
print header, print $user, print $pwd, print end_html; is equivalent to:
print(header, print $user, print $pwd, print end_html); In any programming language, when a function call is encountered in the code, the function is executed, and then the function call is replaced by the function's return value. print() is a function. print() returns 1 if the content was successfully printed. As a result, the last code example is executed like this: 1) perl sees the print() on the left, and perl starts stepping through the list of the things that are supposed to be printed. 2) perl prints the header. 3) print ($user) is a function call, so perl stops doing whatever it was doing and executes the function. Then perl replaces the function call with its return value, which is 1, giving you this: print (header, 1, .....); So perl prints the 1. 4) print($pwd) is a function call, so perl stops doing whatever it was doing and executes the function. Then perl replaces the function call with its return value, which is 1, giving you this: print (header, 1, 1, ...); etc. To correct your problem, you can do either this:
print header, start_html, $user, $pwd, end_html; which is easier to read if you format it like this:
print header, start_html("A page title"), $user, $pwd, end_html, ; And because you should put an html tag around everything you send to a browser, it would be better like this:
print header, start_html("A page title"), div($user), div($pwd), end_html, ; Or you can do this:
print header; print start_html("A page title"); print div($user); print div($pwd); print end_html; You are going to want to use this too:
use CGI::Carp qw{fatalsToBrowser};
(This post was edited by 7stud on Mar 31, 2010, 7:19 AM)
|