user-pic

brian d foy

  • Website: www.theperlreview.com
  • About: I'm the author of Mastering Perl, and the co-author of Learning Perl (6th Edition), Intermediate Perl, Programming Perl (4th Edition) and Effective Perl Programming (2nd Edition).
Subscribe to feed Recent Actions from brian d foy

  • Aristotle commented on Converting glob patterns to regular expressions

    But I like the spread-out version better. :-) I find it much easier to understand what’s going on there.

    And I typically find it easier to reason about stars and pluses than counted repetitions, even if it means I have to count dots.

    So I think I would have written this like so:

    1 while s/[*]+([?]+)[*]*/$1*/g;
     
    s[
        ([?][*]+)
        |([?])
        |([*]+)
        |(\W)
    ]{
        ; $1 ? '.+'
        : $2 ? '.'
        : $3 ? '.*'
        : quotemeta $4
    }xge;

    That is, first, shuffle around question marks while collapsing wildcards as soon as pos…

  • shawnhcorey commented on Converting glob patterns to regular expressions

    Or you could use a hash for the convertion:

    my %convert = (
    '*' => '.*',
    '?' => '.',
    );

    s/(\W)/$convert{$1}||$1/eg;

  • Neil Bowers commented on Converting glob patterns to regular expressions

    You could have a look at glob_to_regex() in Text::Glob.

    I haven't checked it, but if it doesn't handle the cases discussed here, you could submit a PR tomorrow, as part of your CPAN Day celebration :-)

    And while you're there, you could add the github repo to the dist's metadata, so it will appear in the sidebar on MetaCPAN.

  • smls commented on Converting glob patterns to regular expressions

    I wrote this once, for one of my own scripts:

    
    # Given a glob pattern, return a regex that has the following behavior when
    # matched against Cwd::realpath canolicalized path strings:
    #  - matches successfully if and only if the path would have been matched by
    #    the original glob
    #  - $1, $2, ... will contain what would have been matched by groups of
    #    consecutive wildcards (*, ?, [...]) in the original glob
    #  - $::REGMARK will contain the numeric index of the "brace expansion"
    #    alternative in the original glob that would have first matched the path
    # If  tree => 1 …
  • Aristotle commented on Dispatch Tables

    Just put a prefix on the names of functions.

    sub cmd_greet   { print "Hello!\n" }
    sub cmd_inquire { print "How are you?\n" }
    sub cmd_bye     { print "Farewell!\n" }
     
    sub secure_enough_call {
        my ($cmd) = @_;
        my $sub = __PACKAGE__->can("cmd_$cmd")
            or do { warn "No such command $cmd\n"; return };
        $sub->();
    }

    Now if you have a sub delete_all_files there is no way to call it through secure_enough_call.

Subscribe to feed Responses to Comments from brian d foy

About blogs.perl.org

blogs.perl.org is a common blogging platform for the Perl community. Written in Perl and offering the modern features you’ve come to expect in blog platforms, the site is hosted by Dave Cross and Aaron Crane, with a design donated by Six Apart, Ltd.