in reply to Elegant way to parse an optional prefix with default?
I'm not sure it qualifies as elegant, but it does do it in one line for all the cases I thought of.
Update: I just realised that the regex below is more complex than it need be; this works too.
Update2: Removed redundant |& (from the top version)
my($sigil, $name) = ('&'.$_) =~ m/^&?([\$\@%&<*])(.*?)>?$/;
#! perl -sw use strict; for (qw/$foo @foo %foo &foo <foo> *foo foo/) { my($sigil, $name) = ('&'.$_) =~ m/^(?:&?([\$\@%&<*]|&))(.*?)>?$/; print "$sigil:$name\n"; } __END__ c:\test>temp $:foo @:foo %:foo &:foo <:foo *:foo &:foo c:\test>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Elegant way to parse an optional prefix with default?
by John M. Dlugosz (Monsignor) on Nov 08, 2002 at 17:09 UTC | |
by BrowserUk (Patriarch) on Nov 08, 2002 at 18:15 UTC | |
by John M. Dlugosz (Monsignor) on Nov 08, 2002 at 19:39 UTC |