in reply to Re: Random Desktop Background for IceWM
in thread Random Desktop Background for IceWM

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.

Replies are listed 'Best First'.
Re^3: Random Desktop Background for IceWM
by CountZero (Bishop) on Apr 07, 2009 at 05:58 UTC
    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.
        While you're at it, another "Best Practice" is to check "open" returns for failure, to avoid mysterious bugs :
        open my $PREF, '<', $pref or die "Awwww, can't open $pref: $?";