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

Hi Monks! I'm new and this is my first perl question. I have a simple test script like follows:
while(<>){ s/(\w+)/\u\L$1/; print; }
that works fine. as you can guess, it upcases the first letter of the word entered. however, when I run this one-liner in cmd:
perl -p -w -e 's/(\w+)/\u\L$1/'
it just prints the word, with no change (it doesn't capitalize it). they should do the same things, shouldn't they??

Replies are listed 'Best First'.
Re: same script acts differently when run as a file vs one-liner
by ww (Archbishop) on Jul 25, 2012 at 11:19 UTC

    I can't reproduce your problem (on the W7 machine available right now).

    C:\>perl -p -w -e "s/(\w+)/\u\L$1/;" # quoting changed from OP to obl +ige M$ foo Foo ....

    Beyond that, and even though none of this is really on point:

    1. It's not, as your title has it, "the same script." The first version provides an explicit means, <>, of getting a "word" into the "script" while the second uses one version of a 'wrap in a loop' feature ,-p. (BTW, you can concatenate the options as -pwe ... or in 5.14 -- perhaps earlier -- as -pwE to enable features like say. If you do so, the -w becomes unnecessary, since recent versions of Perl -- when executed with features enabled -- automatically invoke use strict; use warnings;.)
    2. The \L in the substitution can be omitted because you use \u which uppercases the first letter only. An upper case \U would say 'uppercase the rest of the word' (and gets Perl to squawk if used together with <c>\L<c>
    Updated: typo fixed
Re: same script acts differently when run as a file vs one-liner
by Anonymous Monk on Jul 25, 2012 at 10:43 UTC
      thanks for the quick reply. however, I didn't really get much use from your code. I read the thread about cmd.exe turning  'print $something;' into "'print" "$something;'" but what's your code supposed to do?
        perl -p -w -e 's/(\w+)/\u\L$1/'

        mavili: What Anonymonk (and, in particular, the given link – did you take a look at it?) wanted to point out is that you are not using the proper command line argument quoting for the Windows command line processor (single-quotes would be appropriate for *nix). Try either
            perl -p -w -e "s/(\w+)/\u\L$1/"
        (using double-quotes) or, because there are no embedded spaces in the CLI argument and thus no need for quotation, simply
            perl -p -w -e s/(\w+)/\u\L$1/
        instead.

        thanks for the quick reply. however, I didn't really get much use from your code. I read ... but what's your code supposed to do?

        Well, its supposed to show you the same thing the other thread showed, what reaches perl and how -- that's of no use?