in reply to Regex stupidity - or, making the easy things hard

(my $module) = $ENV{PATH_INFO} =~ m!^/([^/]+)/; # mind this regex, I'm coming back at it in a few secs!
When I read this, I thought the posting was about the fact that the above is valid code, but the regexp is very unlikely to match a typical PATH_INFO (note that the above code that the terminating regexp delimiter as the last character of the comment!).
my @module_args = $ENV{PATH_INFO} =~ m!/([^/]+)!g;
I would have written it as:
my @module_args = split '/+', $ENV{PATH_INFO};

Replies are listed 'Best First'.
Re^2: Regex stupidity - or, making the easy things hard
by muba (Priest) on Oct 10, 2009 at 02:26 UTC

    Thoughtfull, and perhaps your solution improves readability. But let's consider your approach:

    use strict; use warnings; my $path_info = "/some_module/with/some/args"; my @module_args = split '/+', $path_info; print join(" :: ", @module_args), "\n"; __END__ :: some_module :: with :: some :: args

    Note how there is first an empty element in the array, because the PATH_INFO has a leading slash. Then there is the "real first" part which contains the module name and then come the arguments. So in order to make your solution work, you need two shifts to get the module name. I wonder how useful that is but then again, I might be missing something here.

      (undef, my ($module, @args)) = split '/+', $path_info;
      No shifts.