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

Hello brothers, I have a question about converting case. I would like to open a file, read it in, convert everything to lowercase(except for file handles), and then rewrite the file. Here is what I tried:
$DATA = "my_data_file""; open(FILE, $DATA) || die "Couldn't open: $!"; while(<FILE>){$line = $line.$_;} close(FILE); $line =~ s/(\w+)/\l\l$1/g; open(FILE, ">$DATA") || die "Couldn't open: $!"; print FILE $line; close(FILE);
This was not exactly what I was looking for. It did not seem to lowercase all the letters(especially uppercase letters after an (_ underscore). It also lowercases my filehandles which I don't want. Naming conventions require all filehandles to be at least 4 characters long. I just got a copy of the perl cookbook this morning, and have not had much time to look through it yet. I will, but I thought my fellow monks might have an idea. Any suggestion would be appreciated.
Prince99

Too Much is never enough...

Replies are listed 'Best First'.
Re: upper/lowercase search/replace
by suaveant (Parson) on Apr 16, 2001 at 17:38 UTC
    well... \L lowercases everything after it... \l just does the first letter... use either \L or lc(). I suppose for the filehandle you could do...
    $line =~ s/([A-Z]{4,})/___$1___/g; $line = lc($line); $line =~ s/___([a-z]{4,})___/uc($1)/ge;
    that would work... there are probably better ways, but I'm still asleep... you don't need a regexp for the lc conversion, just running it through lc() should be fine, as I showed.
                    - Ant
Re: upper/lowercase search/replace
by Beatnik (Parson) on Apr 16, 2001 at 17:52 UTC
    one liner based on suaveant's code above...  perl -pi -e's/([A-Z]{4,})/___$1___/g; $_ = lc; s/___([a-z]{4,})___/uc($1)/ge' filename

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
Re: upper/lowercase search/replace
by traveler (Parson) on Apr 16, 2001 at 18:22 UTC
    How do you plan on distinguishing a filehandle from other UC chars? Consider: while(<STDIN>) and print "Press ENTER to continue ";

    traveler

Re: upper/lowercase search/replace
by Rhandom (Curate) on Apr 16, 2001 at 19:47 UTC
    Everything but filehandles? Good luck. You would need a full out parser for that. Even if you had a hash ref of good handles it would still mess those keys up inside of quoted strings. If all you are concerned with is having everthing lower case, forget the handles and do the entire thing with a $_ = lc( $_ );. Lowercase filehandles work to (although they look awful. Also, use use IO::File instead of handles and then you really won't have to worry about your filehandles.

    Ooops, that brings up a good point, you forgot about package declarations, if you are using any other modules (making good use of code reuse), you will mess them up as well.

    By the way, what are you doing this for, the problem might spell a different solution.
      The reason I am doing this is purely a learning experience. A couple of perl script written by a previous employee has upper and lowercase variables. It would seem they are at random.
      (EX.  Claim_Number,  group_number, AWK, PMI, amount_Billed, ...). There seems to be very little rhyme or reason to it. He did however put filehandlles in all uppercase. I was just going over some of his files and wanted to make them more like the shop standard.(all lowercase unless it is a filehandle and filehandles must be at least 4 characters. Thanks for the advice and I will check out IO::File.

      Prince99

      Too Much is never enough...
        Probably what you should do instead is just try to lowercase the variables... words starting in $ % $# @... of course, no matter what you do... its gonna be tough to get it to work 100%
                        - Ant