in reply to Best way to validate a string as a color?

eval {} is your friend :)

#!/usr/bin/perl # http://perlmonks.org/?node_id=1201508 use strict; use warnings; my @colors = qw( red green nosuchcolor blue ); my $defaultcolor = 'black'; use Tk; my $mw = MainWindow->new; for my $color ( @colors ) { eval { $mw->Label( -text => $color, -fg => $color )->pack; 1; } // $mw->Label( -text => $color, -fg => $defaultcolor )->pack; } MainLoop;

Replies are listed 'Best First'.
Re^2: Best way to validate a string as a color?
by pra (Initiate) on Oct 18, 2017 at 11:11 UTC

    I was thinking on other lines! Thank you, eval should do the job.