in reply to Random Desktop Background for IceWM

A few (nit-picking) comments about general coding style (your program wil work OK without applying them, but in larger and more complicated programs, it can make a difference):One final comment: after running your program, you now have an unneccessary "temporary" preferences.tmp file in your system. Perhaps you can have a look at File::Temp that can be used to create and open temporary files in a safe way and these temporary files can be made to disappear automatically once your program finishes.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Replies are listed 'Best First'.
Re^2: Random Desktop Background for IceWM
by Texadan (Monk) on Apr 07, 2009 at 00:47 UTC

    Thank you for taking the time to reply and suggest improvements. Taking them in order:

    I have been trying to remember to use single quotes but I keep forgetting. I will keep working on that.

    If I understand you right, I should do something like this:

    my $pref = "preferences"; my $new_pref = "preferences.tmp"; my $bak = "preferences.bak"; { my $pref = "preferences"; my $new_pref = "preferences.tmp"; my $bak = "preferences.bak"; open PREF, "< $pref"; open NEW_PREF, ">> $new_pref"; while (<PREF>) { s#$image_directory/.*#$wallpaper"#; print NEW_PREF $_; } rename ($pref,$bak); rename($new_pref,$pref); } chdir "/home/jivy" or die "Cannot change to jivy: ($!)"; if (`ps -e | grep icewm-session`) { exec "icewmbg -r"; } else { exec "icewm-session"; }
    Meaning that I don't use specific close PREF and close NEW_PREF commands.

    I had not seen the three-argument form of open but I looked it up after reading your post. That makes sense.

    There is no extra tmp file hanging around. It is renamed to preferences in line 35. But I will do the suggested research for future use.

      What I meant was to use lexical filehandles:
      { open my $PREF, '<', $pref; open my $NEW_PREF, '>>', $new_pref; while (<$PREF>) { s#$image_directory/.*#$wallpaper"#; print $NEW_PREF $_; }
      Once you leave this block, the filehandles go away and the connections to your files are closed.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        Gotcha. Thank you.