in reply to matching a regular expression

Being a "Perl Best Practices" lemming, I think I'd end up with something like this:
#!/usr/bin/perl use strict; use warnings; use IO::Prompt; use Switch 'Perl6'; my $hsbnNet = 'h|hs|hsb|hsbn'; my $adminNet = 'a|ad|adm|admi|admin'; my $prompt = "What network to listen for SNMP traps [hsbn or admin]? " +; my $response = prompt( $prompt, -require => { "Must select either hsbn or admin.\n$prompt" => qr/^(?:$hsbnNet)$|^(?:$adminNet)$/i } ); given ( "$response" ) { when /$hsbnNet/ { ListenOnHsbn(); } when /$adminNet/ { ListenOnAdmin(); } } sub ListenOnHsbn { print "Listening on hsbn Net\n"; } sub ListenOnAdmin { print "Listening on Admin Net\n"; }

Replies are listed 'Best First'.
Re^2: matching a regular expression
by japhy (Canon) on May 18, 2006 at 15:39 UTC
    I've not read PBP, and I don't mean to come off as sounding rude, but how is using Switch -- a source filtering module at heart -- a "best practice"? :|

    And, apart from that, your $hsbnNet and $adminNet values should ideally be generated for you out of "hsbn" and "admin".


    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

      You'd have to try a lot harder for me to think you were being rude...with apologies to Douglas Adams, I eat ruder things than you with my breakfast cereal :)

      The PBP portion of my post was really the IO::Prompt stuff. That early in the morning, I was confusing PBP and "Perl 6 Now" when I pulled in the Switch module. Thanks for pointing it out, I shouldn't have given the impression that the Switch idea was a Damian recommendation.

      As to your other comment, I'm dragging this morning, and am not seeing what you're suggesting. Could you enlighten me?

      Thanks,
      ~jeff

        What I meant was, writing "h|hs|hsb|hsbn" yourself is tedious. Better to have Perl generate that for you:
        my %abbrevs = map { my $word = $_; ($word => join("|", map { substr($word, 1, $_) } 1 .. length($word) +})) } qw( hsbn admin ... );

        Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
        How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart