[Melbourne-pm] An intermittent problem with open for append

Jacinta Richardson jarich at perltraining.com.au
Wed May 28 05:04:00 PDT 2008


Guy Morton wrote:
> aren't you supposed to use "or" instead of "||" after an open, due to  
> operator precedence?
> 
> http://perl.plover.com/FAQs/Precedence.html#Precedence_Traps_and_Surprises

This is correct.  Tim's program will be interpreted as:

	open OUT, (">>/home/foo/que/$ip" || push @error, "Cant save details");
	print OUT "$ip:t:date=",scalar localtime,"\n";

which means that the push will only occur if ">>/home/foo/que/$ip" is false -
which it won't be.  The correct file will be opened for appending however.
Since the program isn't dying on an error, this just means that Tim's
diagnostics will be ignored.  Since he's then going to try printing to the
possibly not-opened file handle ANYWAY, I suspect he doesn't care too much.

In this kind of instance, I'd recommend:

	if(open OUT, ">>/home/foo/que/$ip") {
		print OUT "$ip:t:date=",scalar localtime,"\n";
	}
	else {
		push @error, "Can't save details";
	}

as this ensures both the correct precedence and removes the warning (you have
warnings turned on right?) about printing to an unopened filehandle in the case
of an error.

I don't think this is the cause of Tim's current problem, but it could be the
cause of an error in the future.

All the best,

	J

-- 
   ("`-''-/").___..--''"`-._          |  Jacinta Richardson         |
    `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
    (_Y_.)'  ._   )  `._ `. ``-..-'   |      +61 3 9354 6001        |
  _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
 (il),-''  (li),'  ((!.-'             |   www.perltraining.com.au   |


More information about the Melbourne-pm mailing list