Cody Pendant has asked for the wisdom of the Perl Monks concerning the following question:

Here's a little routine to wrap text in HTML:

$text = 'Sunt autem quidam e nostris, qui haec subtilius velint tradere et negent satis esse. Quid bonum sit aut quid malum, sensu iudicari. Sed animo etiam ac ratione intellegi posse et voluptatem ipsam per se esse expetendam et dolorem ipsum per se esse fugiendum. '; $text = '<p class="first">' .$text . '</p>'; $text =~ s|(\n\n+)|</p>\1<p class="later">|g; print $text;

And it works just fine.

My next though was "I wonder if I can do both in one statment, and I tried various things, for instance:

$text = '<p class="first">' . ($text =~ s|(\n\n+)|</p>\1<p class="late +r">|g) . '</p>';
but they didn't work.

Any ideas? Can it be done in one statement?
--

($_='jjjuuusssttt annootthhrer pppeeerrrlll haaaccckkeer')=~y/a-z//s;print;

Replies are listed 'Best First'.
Re: Concatenation and Regex on one Statement?
by dws (Chancellor) on Mar 16, 2002 at 01:04 UTC
    Any ideas? Can it be done in one statement?

    Indeed it can, thanks to the utility of do.

    $text = '<p class="first">' . do { $text =~ s|(\n\n+)|</p>\1<p class="later">|g; $text } . '</p>';
      Interesting.

      What's the difference between "do" and "eval" here?
      --

      ($_='jjjuuusssttt annootthhrer pppeeerrrlll haaaccckkeer')=~y/a-z//s;print;
        What's the difference between "do" and "eval" here?

        Both "do" and "eval" return the value of last expression in the block. "eval" traps exceptions, capturing the argument to "die" and stuffing it into $@. "do" doesn't. I prefer it as a lighter-weight solution to problems like the one you posed.

Re: Concatenation and Regex on one Statement?
by abstracts (Hermit) on Mar 16, 2002 at 01:57 UTC
    Hello

    Here is another way to do it:

    #!/usr/bin/perl -w $text = <<EOF; Sunt autem quidam e nostris, qui haec subtilius velint tradere et negent satis esse. Quid bonum sit aut quid malum, sensu iudicari. Sed animo etiam ac ratione intellegi posse et voluptatem ipsam per se esse expetendam et dolorem ipsum per se esse fugiendum. EOF $text = join '', map qq{<p class="first">$_</p>\n}, split /\n{2,}/, $text; print $text;
    Aziz,,,
Re: Concatenation and Regex on one Statement?
by trs80 (Priest) on Mar 16, 2002 at 05:20 UTC
    use CGI qw/:standard/; use strict; my $text = 'Sunt autem quidam e nostris, qui haec subtilius velint tradere et negent satis esse. Quid bonum sit aut quid malum, sensu iudicari. Sed animo etiam ac ratione intellegi posse et voluptatem ipsam per se esse expetendam et dolorem ipsum per se esse fugiendum. '; my $class = 'first'; foreach (split(/\n{2,}/,$text)) { print p({class=>$class},$_) , "\n"; $class = 'later'; }
Re: Concatenation and Regex on one Statement?
by Cody Pendant (Prior) on Mar 16, 2002 at 04:10 UTC
    Thank you all for your comments.

    Why did I want to do it in one statement? I didn't, but I just wanted to know what was wrong with my Perl knowledge that I couldn't figure out how to do it, that's all.

    I've learnt something useful and portable about executing a block inside a statement, so that's cool.

    And, nothing wrong with the map version of it, but just note that it doesn't do what mine does, because my first par is marked as such, (the later pars have a different class). This is a standard in applications which share news stories via XML, or so my friends in online news tell me.
    --

    ($_='jjjuuusssttt annootthhrer pppeeerrrlll haaaccckkeer')=~y/a-z//s;print;
Re: Concatenation and Regex on one Statement?
by gav^ (Curate) on Mar 16, 2002 at 02:38 UTC
    Why? What's wrong with it being 2 statements?

    gav^