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

Discussion in 'Perl Misc' started by Ken Sington, Jul 10, 2004.

  1. Ken Sington

    Ken Sington Guest

    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;
     
    Ken Sington, Jul 10, 2004
    #1
    1. Advertising

  2. Ken Sington

    Paul Lalli Guest

    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
     
    Paul Lalli, Jul 10, 2004
    #2
    1. Advertising

  3. Ken Sington

    gnari Guest

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

    "Ken Sington" <ken_sington@nospam_abcdefg.com> wrote in message
    news:...
    > 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
     
    gnari, Jul 10, 2004
    #3
  4. Ken Sington <ken_sington@nospam_abcdefg.com> wrote in
    news::

    > 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/Object_Headers.html#z16


    --
    A. Sinan Unur
    (reverse each component for email address)
     
    A. Sinan Unur, Jul 10, 2004
    #4
  5. Ken Sington

    J. Romano Guest

    Ken Sington <ken_sington@nospam_abcdefg.com> wrote in message news:<>...
    > 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.
     
    J. Romano, Jul 11, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jürgen Exner

    Re: CGI Perl "use CGI" statement fail

    Jürgen Exner, Jul 31, 2003, in forum: Perl
    Replies:
    0
    Views:
    1,622
    Jürgen Exner
    Jul 31, 2003
  2. Shailan
    Replies:
    2
    Views:
    1,162
    Shailan
    Dec 15, 2003
  3. John Smith
    Replies:
    0
    Views:
    3,246
    John Smith
    May 15, 2006
  4. keto
    Replies:
    0
    Views:
    1,049
  5. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    402
    David Cournapeau
    Dec 30, 2008
Loading...

Share This Page