sub_chick has asked for the wisdom of the Perl Monks concerning the following question:

I was practicing random one-liners on this rainy christmas day and I came across one that I can't seem to get right.
perl -e 'if('42'=~ /[:^digit:]/) {print "42 is a digit\n"}; else {prin +t "42 is not a digit\n"}';
And the output is
> > >
and no shell prompt. I am sure its something simple I am just not seeing.
Thanks

"Es gibt mehr zu Leben als Bücher, kennen Sie. Aber nicht viel mehr " -(Der Smiths)

Replies are listed 'Best First'.
Re: POSIX-Style character classes
by borisz (Canon) on Dec 25, 2005 at 19:33 UTC
    Perhaps you want:
    perl -e 'if("42"=~ /^[[:digit:]]+$/) {print "42 is a digit\n"} else {p +rint "42 is not a digit\n"};'
    the inner [] is part of the charclass name. And do not mix ' and " for oneliners. Just start with ' and use " or q or qq in your oneliner on UNIX.
    Boris
Re: POSIX-Style character classes
by ww (Archbishop) on Dec 25, 2005 at 21:42 UTC
    I would argue that (quoting for windows users; language pedantry re digits for any who stumble upon this):
    C:\>perl -e "if('42'=~ /^[[:digit:]]$/) {print qq(42 is a digit\n)} else {print qq(42 is not a digit\n)};"
    which prints
    42 is not a digit
    is actually correct, given that while "4" and "2" are digits, "42" is NOT.

    However, if your intent was to test that the "42" is comprised exclusively of digits, then,

    C:\>perl -e "if('42'=~ /^[[:digit:]]+$/) {print qq(42 is exclusively d +igits\n)} else {print qq(42 is not a digit\n)};"
    accurately prints:   "42 is exclusively digits"
    because the second 1-liner above tests for one_or_more digits.

Re: POSIX-Style character classes
by HuckinFappy (Pilgrim) on Dec 26, 2005 at 03:33 UTC
    I see two problems:
    1. The mixing of ' and ". Your single quotes around the '42' are the first thing breaking you
    2. The semicolon afte the if { } and before the else
    fappy@flux[62] tmp > perl -e 'if( q{42} =~ /[:^digit:]/) {print "42 is + a digit\n"} else {print "42 is not a digit\n"}'; 42 is not a digit

    Now the questions remainns, what did you really want to test?

Re: POSIX-Style character classes
by TedPride (Priest) on Dec 26, 2005 at 02:56 UTC
    Somewhat unrelated, it's neater to say:
    print "42" =~ /^[[:digit:]]+$/ ? "42 is a digit" : "42 is not a digit" +;