in reply to [Solved]Newbie - convert carriage return in a variable

I have tried with HTML::Parser

I wonder why, since there is no HTML to parse. Instead see this solution using s///:

use strict; use warnings; use Test::More tests => 1; my $have = 'foo bar'; my $want = 'foo<br />bar'; $have =~ s/\n/<br \/>/; is $have, $want;

Hopefully you will see here that I have included nothing which is superfluous to the task at hand.

Replies are listed 'Best First'.
Re^2: Newbie - convert carriage return in a variable
by Balawoo (Novice) on Feb 17, 2019 at 17:50 UTC
    Thanks hippo, In this case the script will just replace the first end of line found. If I want to replace more than one as
    use strict; use warnings; use Test::More tests => 1; my $have = 'foo bar'; my $want = 'foo<br /><br />bar'; $have =~ s/\n/<br \/>/; is $have, $want;
    I think I need to us chomp function but It's not clear how to use it.
    use strict; use warnings; use Test::More tests => 1; my $have = 'foo bar'; my $want = 'foo<br /><br />bar'; $have =~ s/\n/<br \/>/; is $have, $want; my @data=split(/\n/<br \/>/); foreach my $have (@data) { $want = $have; }
    But it's not working :(

      Add the g modifier to your regex

      #!perl use strict; my $text = 'foo bar'; $text =~ s/\n/<br \/>/g; print $text;
      poj
        Thanks @poj It's solved