Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > CGI: print $x =~ s/\n/<br>\n/g;

Reply
Thread Tools

CGI: print $x =~ s/\n/<br>\n/g;

 
 
Ken Sington
Guest
Posts: n/a
 
      07-10-2004
In this test:

#!/usr/bin/perl -T
print "content-type: text/html \n\n";

use warnings;
use strict;

use CGI qw/param/;
my $x=param("x");

print qq{
<form method="GET">
<textarea name="x" cols="50" rows="20">$x</textarea><br>
<input type="submit">
</form>
};


print "<hr>\n";

my $s=length($x);
print $s;
print "<br>\n";

print $x =~ s/\n/<br>\n/g; #This one here...



=================


This: print $x =~ s/\n/<br>\n/g;

doesn't do this:
$x =~ s/\n/<br>\n/g;
print $x;
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      07-10-2004
On Fri, 9 Jul 2004, Ken Sington wrote:

> This: print $x =~ s/\n/<br>\n/g;
>
> doesn't do this:
> $x =~ s/\n/<br>\n/g;
> print $x;



No, it doesn't. You're printing the return value of the substitution.
That is, you're printing the return value of the s/// operation. The
substitute operator returns the number of substitutions it made. It does
not return the new value of the binded variable.

Another way to illustrate:

$x = "123def789";
$num = ($x =~ s/[a-z]/#/g);
print "Substitutions: $num\n";
print "String: $x\n";

This prints 3 first, because the substitution happened three times (one
for each letter it replaced). It then prints the new string, '123###789'

Paul Lalli
 
Reply With Quote
 
 
 
 
gnari
Guest
Posts: n/a
 
      07-10-2004
"Ken Sington" <ken_sington@nospam_abcdefg.com> wrote in message
news:(E-Mail Removed)...
> In this test:


[snip code that emits malformed html]

> This: print $x =~ s/\n/<br>\n/g;
>
> doesn't do this:
> $x =~ s/\n/<br>\n/g;
> print $x;


that's right. and what's more, this is true even
in a non-CGI situation.

the print prints the result of the s///g operation,
which is not the $x

gnari







 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      07-10-2004
Ken Sington <ken_sington@nospam_abcdefg.com> wrote in
news:(E-Mail Removed):

> In this test:
>
> #!/usr/bin/perl -T
> print "content-type: text/html \n\n";


Another reason to use CGI.pm. See:

http://www.w3.org/Protocols/HTTP/Obj...aders.html#z16


--
A. Sinan Unur
http://www.velocityreviews.com/forums/(E-Mail Removed) (reverse each component for email address)
 
Reply With Quote
 
J. Romano
Guest
Posts: n/a
 
      07-11-2004
Ken Sington <ken_sington@nospam_abcdefg.com> wrote in message news:<(E-Mail Removed)>...
> In this test:
>
> This: print $x =~ s/\n/<br>\n/g;
>
> doesn't do this:
> $x =~ s/\n/<br>\n/g;
> print $x;


You are correct. Your confusion probably comes from the fact that
the line:

print $x =~ s/\n/<br>\n/g;

looks like the line:

print $x = 5;

but behaves nothing like it.

When you say "$x = 5", the "=" operator changes the value of $x and
returns the same value (so it is the value of $x that gets printed
with the print() function). The "=~" operator, on the other hand,
changes the value of $x but does NOT return the value of $x.
According "perldoc perlop", it returns the number of substitutions
made (when used with s///), which is rarely what it sets $x to.

Therefore, the number of substitutions made is what gets passed to
the print() function, instead of the new value of $x, as you had
thought.

I hope this helps,

-- J.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
print a vs print '%s' % a vs print '%f' a David Cournapeau Python 0 12-30-2008 03:19 AM
Problem - I want to print Current Output of Pdf file and should print once.I get print dialog box but it is not working keto Java 0 05-30-2007 11:27 AM
Can't print to print server =?Utf-8?B?cGVjaw==?= Wireless Networking 2 02-03-2005 02:41 PM
How to make use of "print to file" option by print =?Utf-8?B?enljZ3M=?= MCSE 1 01-04-2005 06:50 PM
Unlarging the print to print using PDF file to print Bun Mui Computer Support 3 09-13-2004 03:15 AM



Advertisments