in reply to Extract substring from string with no whitespace using regexp?
Or is it just meant to be equivalent to the following?if (!$ARGV[0]) { print "Enter launch site! (don't forgot to include the \"http://\") +\n"; exit(0); } while (1) { exit(0) if(($target) && ($target eq $target)); $target = $ARGV[0] unless($target); ...
The point is that you don't need a while loop, especially not one that suggests iteration will continue indefinitely until some condition is met -- personally, I find the latter version more appropriate (and easier). Also, I can't imagine any situation where "$target eq $target" could ever be false (given that $target is a simple scalar string), so I don't understand why you would test that.my $Usage = "$0 http://target.url\n"; die $Usage unless ( @ARGV == 1 and $ARGV[0] =~ m{^http://} ); $target = shift; ...
As another aside: some users might be grateful if it didn't insist that they type "http://" (and why not support "ftp://" as well) -- normal browsers have this flexibility:
This version uses a grouping expression (?:http|ftp) which does not assign the matched region to a "capture variable" ($1, $2, etc); but if such a region is followed by "://", then that whole string (including the slashes) is assigned to $1. But even when nothing matches that first part of the regex, the remainder of the string (if any, and if it begins with something that matches \w) is always captured into $2. The assignment to $target is the whole arg if both capures were non-empty; otherwise, a suitable prefix is applied to $2 -- lots of ftp servers are using "ftp." as the first first part of the actual host name, whereas web servers now answer to "site.dom" as well as "www.site.dom".die $Usage unless (@ARGV == 1 and $ARGV[0] =~ m{^((?:http|ftp)://)?(\w +*.*)}i and $2); $target = ( $1 ) ? shift : ( substr($2,0,3) eq "ftp" ? "ftp://$2" : " +http://$2"; ...
The output of "perldoc perlre" is very long, but very rewarding to the careful reader. Browsing through that man page is time well spent.
|
|---|