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

Hello Monks!

Finally had an occasion to use perl for something other than to complete an exercise in the llama! Still making newbish mistakes though.

The idea here is to parse some config files that reveal my database password and replace the real password with a fake password. Also does the same to hide the name of the database user. I call the program "cleaner". :)

I think I'm close to getting it to work. It does alter the username and password in the output. But when I examine the original files with cat the file is unchanged.

I think I need to select the file for output, but I am a little unsure of the usage. I'd appreciate any advice you might have.

Here's the program in question:
#!/usr/bin/perl -w use strict; foreach (@ARGV) { while (<>) { s/\s+myOldPass/ Onl33In05h15notmyreallpass/g; s/\s+myDBUser/ my-secret-db-user/g; print; } }

My ultimate goal is to be able to safely pastebin my config files to IRC so that some postfix genius can help me figure out why my postfix setup is not talking to MySQL.

Thanks guys! Very excited to be using Perl for a real world application for the first time!

Replies are listed 'Best First'.
Re: Files not altered
by zwon (Abbot) on Jan 04, 2009 at 14:32 UTC

    Try this:

    #!/usr/bin/perl use strict; use warnings; $^I = '~'; while (<>) { s/\s+myOldPass/ Onl33In05h15notmyreallpass/g; s/\s+myDBUser/ my-secret-db-user/g; print; }

    See perldoc perlvar for description of the $^I variable. Alternatively you can use -i switch for perl.

    Update: note also that you don't need foreach to loop through @ARGV. <> will loop automatically.

    Update2: fixed extension

Re: Files not altered
by zentara (Cardinal) on Jan 04, 2009 at 14:54 UTC
    Just looping thru a file, will not edit it's data on the harddrive. You need to open the file, write your changes, the save it. What you are doing is working on a temp copy of the file. See Compact multi-line inplace file edit search and replace or google for "perl in place edit" for many other recipes. As a caveat, and what you are trying, there is a shortcut available by working on files in @ARGV, but in general, you open, write and save. The @ARGV stuff is sort of Perl magic, and not the general method for modifying files.

    I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: Files not altered
by Tanktalus (Canon) on Jan 04, 2009 at 22:20 UTC

    Just a suggestion on the goal: you probably don't want to modify the file on disk anyway. You really want to pastebin the config file anyway, so I would suggest the following design:

    1. Slurp in the file given on the commandline (just one per execution).
    2. Modify it with your regexes.
    3. Using WWW::Mechanize, paste it to your favourite pastebin site. Use the return page to determine the URL it's now sitting at.
    4. Print the URL (for copy-and-paste), and, if you can, put it in the clipboard (how to do that depends on your OS - for Unix, I'd just open a pipe to xclip and let that do my work for me)
    This not only makes it safer (by not overwriting your real config files by accident), but also far more convenient. And there's lots to learn about perl in there, too :-)

Re: Files not altered
by balakrishnan (Monk) on Jan 05, 2009 at 05:07 UTC
      I had a look at the linked node, but I can't see the connection with this question (other than superficially "content not appearing in files"). Could you explain further?

      --
      use JAPH;
      print JAPH::asString();

        It may not be exactly related to your requirement.

        I'm trying to convey that when you see that file is not updated, there may be a case that the file handle not selected for automatic flushing. In such a case, you can use the select to make the file updation proper.

        This is just to give you some clarification in the "files not altered" matter.
        It isn't relevant (yet) since you aren't writing to a file in your example. Once you do get that straightened out, autoflush can be useful if you are trying to look at your output file while the program is still running.
Re: Files not altered
by Anonymous Monk on Jan 07, 2009 at 16:28 UTC
    You aren't modifying files.
    <> reads from STDIN print; writes to STDOUT
    You need to either explicitly open an output filehandle and print to it in the foreach loop or use the "-i" magic mentioned above.
Re: Files not altered
by Ciclamino (Sexton) on Jan 08, 2009 at 16:14 UTC
    I particularly like the module IO::InSitu for this sort of thing.