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:
Why this simple script showing wired output

 



expertalert
New User

Mar 31, 2010, 2:08 AM

Post #1 of 3 (392 views)
Why this simple script showing wired output Can't Post

Hi bellow is a simple script.

after putting usernanme:fosiul, password: pass
the out put is coming like this :
pass1fosiul11

Or

demopass1demon11

can any one please tel me what causing this problem??
thanks for your time and help


Code
 
#!/usr/bin/perl -T
use strict;

use CGI qw/:standard/;

my $data = new CGI;

my $user = $data->param('name');
my $pwd = $data->param('pass');

if($user ne '')
{

print header,
print $user,
print $pwd,
print end_html;
exit;
}

else
{
print header,
start_html('Hello'),
start_form(-method=>'POST',-action=>"/cgi-perl/mylogin.pl"),
"Enter your name: ",textfield('name'),
"Enter your Password: ",textfield('pass'),
submit,
end_form,
hr,


}



Nila
User


Mar 31, 2010, 4:06 AM

Post #2 of 3 (383 views)
Re: [expertalert] Why this simple script showing wired output [In reply to] Can't Post

Try this,


Code
if($user ne '')  
{

print header,
$user,
$pwd;
print end_html;
exit;
}


If you want format output you can use header tag like,

Code
h1($user,$pwd);



7stud
Enthusiast

Mar 31, 2010, 7:00 AM

Post #3 of 3 (374 views)
Re: [Nila] Why this simple script showing wired output [In reply to] Can't Post

This:


Code
print header,  
print $user,
print $pwd,
print end_html;


is equivalent to:


Code
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:


Code
print  header,  start_html,  $user,  $pwd,  end_html;


which is easier to read if you format it like this:

Code
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:


Code
print   
header,
start_html("A page title"),
div($user),
div($pwd),
end_html,
;


Or you can do this:


Code
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:


Code
use CGI::Carp qw{fatalsToBrowser};



(This post was edited by 7stud on Mar 31, 2010, 7:19 AM)

 
 


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

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