Solved

Why perl program output this ?

Posted on 2003-10-26
3
228 Views
Last Modified: 2010-03-05
Hi,

I've written below perl program for testing in my web site hosted in one ISP.

Source code below:

#!/usr/bin/perl

$a = "Hello!";
print "Content-type: text/html\n\n";
print "<html>\n<head>\n";
print "</head>\n<body>\n";

$e = "<xxxx>";
$e =~ s/<//;
print "variable e = $e<br>";
$e =~ s/>//;
print "variable e = $e<br>";

$f = "<xxxx>";
$f =~ s/>//;
print "variable f = $f<br>";
$f =~ s/<//;
print "variable f = $f<br>";

print "</body>\n</html>";

Output of the program at browser is as follow:

variable e = xxxx>
variable e = xxxx
variable f = variable f = xxxx

Why will 2 distnct print statements for $f will result in only one output line ? Why the second print statement seems overwriting the first ? Is there any meaning to character '<' if it appears alone in a " " quoted string i.e. "<xxxx" ?

0
Comment
Question by:lonelyplanet
3 Comments
 
LVL 10

Accepted Solution

by:
rj2 earned 100 total points
ID: 9623474
Replace lines "$f =~ s/>//" and "$f =~ s/<//" with line below.
$f =~ s/[<>]//g;

This happen because when there is a starting < browser interpret that as a start tag, and when the <br> comes it is ignored because there can't be any nested tags.
0
 
LVL 8

Assisted Solution

by:jhurst
jhurst earned 25 total points
ID: 9675994
&lt; is the correct way of outputting a < to an html screen!
0
 
LVL 20

Expert Comment

by:jmcg
ID: 10038148
Nothing has happened on this question in over 2 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
split points between rj2 [100 ptt] and jhurst [25 pts].

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question