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.
  • And with Term::ANSIColor [perladvent.org] you don't even have to remember or look up what the code for red is :-)

    (I know...why complicate a perfectly simple thing :)

    • Here's a version that uses Term::ANSIColor and takes a -c option to define the colour used.

      #!/usr/bin/perl

      use Term::ANSIColor ':constants';
      use Getopt::Std;

      my %opts;
      getopts('c:w:', \%opts);

      $opts{w} ||= shift;
      $opts{c} = eval $opts{c} || RED;

      while (<>) {
        s/\Q$opts{w}\E/$opts{c} . $opts{w} . RESET/ge;
        print;
      }

      Might want to add some code checking the contents of $opt{c} before running "eval" on it.

      • I wonder why everyone uses the :constants interface and eval()s their command line parameters. I've seen this meme in half a dozen different places now. The module has a perfectly good functional interface that makes this use case clearer too:

        #!/usr/bin/perl -w
        use strict;

        use Term::ANSIColor;
        use Getopt::Std;

        getopts( 'e:c:', \my %opts );

        my $rx = $opts{e} || shift;
        my $color = $opts{c} || 'bold red';

        while( <> ) {
            s{ ( $rx ) }{ colored( $1, $color ) }gex;
            print;
        }