And the clouds parted, and Kevin Pfeiffer said... > > > > Here's a line that will give you exactly that. > > ($PROGNAME = $0) =~ s|(\.?\.?(/[^/]+)*/)?([^/]+)|$3|; > > I have two questions... > > I came up with this earlier today: > my ($program) = $0 =~ /([^\/]+)$/; > > Doesn't it do the same? (Question 1)
Sure looks that way, yes. :) I realized after you posted your (much, _much_) simpler regex that the reason mine was so convoluted was that the script I was using it in originally needed to capture the path the script was invoked with as well as the script name. For just the script name your solution should perform admirably. > If I don't escape the slash in the char class -- i.e. /([^\/]+)$/ -- I get > this error: > Unmatched [ in regex; marked by <-- HERE in m/([ <-- HERE ^/ at ./test-0 > line 7. > > This makes no sense to me (since this is Perl and not sed or something)... > (implied Question 2) The reason is that you're using '/' as the delimiter for the m// operator, and '/' isn't automatically escaped in a character class (it's not a metacharacter). I got away with it because I was using '|' as the delimiter in my substitution. What I mean is this. Talking about the m// and s/// operators, you can use just about anything in place of the '/'s. (We won't discuss using the 'x' option to m// and s///, for the sake of brevity and simplicity.) m/pattern/ #is the same as m;pattern; #which is the same as m|pattern| and s/pattern/replacement/ #is equivalent to s#pattern#replacement# ...or even... s{pattern}{replacement} ...but I digress. :) So to avoid what the Camel Book refers to as 'leaning toothpick syndrome' when matching unix pathnames, I use a delimiter other than '/' so that I don't have to always type '\/' when I want to match a slash in the path. As for why it wouldn't lose its "specialness" inside of a character class, that only applies to regex metacharacters ('.', '*', '+', etc). When perl looks for the pattern to match it looks for everything up to the first matching delimiter before trying to compile it. So when you said m/([^/]+)$/ perl tried to use ([^ as its regex, which isn't valid. That's why it was complaining about an unmatched [. It only saw one. :) HTH- Brian /~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\ | Brian Gerard "I am Pooh of Borg. Bother is | | First initial + 'lists' futile; hunny will be assimilated." | | at technobrat dot com | \______________________________________________________________________/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]