hperange has asked for the wisdom of the Perl Monks concerning the following question:
And I am trying to color the output of a system command. My code is working, but I wonder whether the below last 2 lines can be joined into a single statement:my @rules = ( { name => 'name1', re => qr/regex1/, color => 'yellow', }, { name => 'name2', re => qr/regex2/, color => 'green', }, { name => 'default', re => qr/.*/, color => '', }, );
I am asking because I am not sure whether it's good practice that in the first line $color holds a reference to a hash, and in the next line $color holds a string. Thanks for your time.#!/usr/bin/perl use strict; use warnings; use Term::ANSIColor; use List::Util qw(max sum first); ... $class = '...'; $color = first { $class =~ /$_->{'re'}/ } @rules; $color = $color->{'color'};
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Data structure / hash reference question
by roboticus (Chancellor) on Apr 21, 2012 at 14:22 UTC | |
by stevieb (Canon) on Apr 21, 2012 at 14:36 UTC | |
by roboticus (Chancellor) on Apr 21, 2012 at 18:07 UTC | |
by stevieb (Canon) on Apr 21, 2012 at 18:42 UTC | |
by tobyink (Canon) on Apr 22, 2012 at 08:45 UTC | |
by stevieb (Canon) on Apr 22, 2012 at 13:10 UTC | |
by hperange (Beadle) on Apr 21, 2012 at 15:27 UTC |