Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • Just look at it:

    msg = ' spacey string ';
    msg.replace(r'^\s+', '');
    msg.replace(r'\s+$', '');

    That’s clearly orders of magnitude easier to read than the Perl version.

    • Oh wait, it has to be this:

      msg = ' spacey string ';
      msg = msg.replace(r'^\s+', '');
      msg = msg.replace(r'\s+$', '');

      Or possibly chain the methods.

      msg = ' spacey string ';
      msg = msg.replace(r'^\s+', '').replace(r'\s+$', '');

      Perl can’t hold a candle to any of that.

      • Perl can’t hold a candle to any of that.

        But perl6 might...
        http://xrl.us/ftqq [xrl.us]
        http://xrl.us/5obh [xrl.us]

        In Python that can be written as:
        msg = msg.strip()
        • In Python that can be written as:
          msg = msg.strip()
          Hey, but that's nearly impossible to write in Perl. Let me see:

          # in-place version
          sub strip { s/^\s+//; s/\s+$//; }
          # which would be used as
          strip($msg);
          • That won’t quite work. Make it

            sub strip { $_[0] =~ s/^\s+//; $_[0] =~ s/\s+$//; }

            or maybe

            sub strip { s/^\s+//, s/\s+$// for $_[0] }
            • use strict;
              use warnings;

              my $string = " This is the test ";

              sub trim {
                      my $a = $_[0];
                      $a = join(" ", split(" ", $a));
                      return $a;
              }

              my $nstring = trim($string);

              print "$string\n";
              print "$nstring\n";

              I don't think it takes every case...but it works with the "test" string.