Hi John, On Monday 07 Feb 2011 16:18:32 John Delacour wrote: > At 14:22 +0200 07/02/2011, Shlomi Fish wrote: > >Hi John, > > > >a few comments on your code. > > > > > >Actually, see perldoc perlrun - http://perldoc.perl.org/perlrun.html - by > >giving -p and -i (untested) you can replace the contents of a file > >"in-place". > > untested?! Why don't you test it before recommending it to others? >
Well, I apologise for not testing the exact incantation. I overlooked that. I've used the -p and -i flags for a long time now, having utilised this knowledge in Perl golf competitions, and found it of use in my command line work. Here's a working example, although a bit silly one: [quote] shlomif:~$ cat test.txt Hello World! shlomif:~$ perl -p -i.bak -e 's/World/Friend/' test.txt shlomif:~$ cat test.txt Hello Friend! [/quote] cat is a UNIX command, so if you're working on a non-UNIX system, then use the equivalent command (and single-quotes are not supported by CMD.EXE, etc.). I should note that it's not the first time I've seen people use ("untested") when giving code samples. "Beware of bugs in the above code - I have only proven it correct, not tested it." -- Don Knuth. > >Now for some comments on your code. > > You said that already. > I apologise for being redundant. > > > #!/usr/bin/perl > >> > >> use strict; > > > >Add "use warnings;" too. > > I certainly will if I need them, Shlomi. Why do you feel they are not needed in this case? > > > > my ($fin, $fout) = @ARGV; > > > >It's great that you unpack @ARGV like that instead of using $ARGV[0], > >$ARGV[1] etc. But please say something like $in_fn and $out_fn or > >something like that. > > Thanks for the kudos. I fail to see how "something like $in_fn" is > any more or less obscure than $fin. Well, fn is a common abbreviation for filename. On the other hand the f in "fin" can mean file-handle, file name, file, contents of a file, etc. I recommend avoiding calling things file: http://perl-begin.org/tutorials/bad-elements/#calling-variables-file > > >> open FIN, $fin; > >> open FOUT, ">$fout"; > > > >1. Don't use bareword filehandles. > > Don't just tell us; give us an example of an unbareword filehandle > and tell us why it's universally preferable. > > >2. Use three-args-open. > > Ditto. If you want arguments you've come to the right place. > > >3. Always append or die. > > Fair enough, I normally do even when, as in my example, I am not appending. > I meant add the text after the open the «open my $fh, ">", $filename or die "Cannot open the file '$filename' for appending";» , not necessarily when opening a file in append mode. > > > while (<FIN>) { > > > >To avoid $_ getting tempered with it's a good idea to use an explicit > >$line variable. > > You may think so and thousands of others may not. Granted. I agree that in quick and dirty scripts, I sometimes use $_ (but often end up regretting it), but I've explained why using a lexical variable is better than using $_ which can be changed implicitly and explicitly in many ways. $_ is useful when being used in built-in functions such as grep or map, and in related functions as those provided by List::Util , List::MoreUtils and List::UtilBy , but even then sometimes it's a good idea to assign it to a lexical (especially if you nest such functions.) > And while you're > at it, get yourself a decent mail program that does format=flowed. > > <http://www.ietf.org/rfc/rfc3676.txt> > It's the first time I've heard of format=flowed. I've heard very few complaints about my E-mails even from very netiquette conscious people, since I'm using KDE's KMail, which is very standards compliant. Nevertheless, I'll try to investigate about this format=flowed thing and see if KMail can be made to support it. Part of KMail's appeal to me is that it handles Unicode and Bidirectionality very well, which is important for me as an Israeli who needs to communicate in both the Hebrew and Latin alphabets (and may sometimes need to handle Arabic as well). And I've seen many "crimes" against E-mail netiquette recently from many people. > and since I'm subscribed to the list, I don't need a private copy of > the message either. > Well, I apologise for that. I realise that whether to CC one on a reply to his post to a mailing list is an ongoing debate and that some people prefer it this way and the others prefer it the other way. The reason I CC people on their replies is primarily because by default I hit "A" on KMail to reply to all which is useful in case there are several mailing lists in the recipients, or it's a private E-mail with several recipients. If I want to reply in private, I hover over the email address with my mouse, invoke the context menu, and hit reply to all. One thing that annoys me about the GMail.com web- interface is that by default it replies only to the original recipient and not to all recipients, which tends to end up in my inbox, even if it was intended for public consumption. This is why I have the last line in my signature, and why when I find a need to reply in private, I normally mention it in my E-mail so people won't think I did it by accident (and would recommend everyone to do the same.) I also actually appreciate a copy arriving at my inbox and many people will prefer it too, and I don't feel this issue is as clear-cut as most others. Regards, Shlomi Fish -- ----------------------------------------------------------------- Shlomi Fish http://www.shlomifish.org/ "Humanity" - Parody of Modern Life - http://shlom.in/humanity Chuck Norris can make the statement "This statement is false" a true one. Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/