If you keep your replies on the list, you'll get help from all the people much smarter than me. ;)

On Friday, October 3, 2003, at 04:09 PM, Dean Do wrote:

> # sort /etc/passwd > /etc/passwd-new
> # mv /etc/passwd-new /etc/passwd

Any good reason not to use the above?� :)��

[dean] Thanks for your response, James.� The thing is... I have an existing Perl script that I working on and the above UNIX shell commands won't work in the Perl script.� Is there a way to make them work?

Yes. Try:


`sort ...`;
`mv...`;

Generally, this is considered a poor solution, because it's not very portable. I'll leave you to decide how important that is in this case.

Well, what would be the steps?

1. open /etc/passwd/
2. read all entries into memory
3. close /etc/passwd/
4. sort entries
5. open new file
6. print all entries
7. close new file
8. rename new file

Can you walk down that list replacing it with Perl statements?��

[dean] That's a lot more steps to accomplish a simple sort of a text file.� I can replace those steps with Perl statements.� Thanks again for your help.

Maybe, but it's really not hard to code up and it's much more flexible. You can also shorten it up a little, if it's a big concern:


open OLD, '/etc/passwd' or die "File error:  $!\n";
my @entries = sort grep !/^#/, <OLD>;
open NEW, '>/etc/new_passwd' or die "File error:  $!\n";
print foreach @entries;
close NEW;
rename '/etc/new_passwd', '/etc/passwd';

Hope that helps.

James

Reply via email to