Why can I open undef?

The following does not die:

open my $in, '<', undef or die q{couldn't open undef}; print <$in>; 

Also this:

 open my $in, '>', undef or die q{couldn't open undef}; print $in 'hello'; 

I don’t understand why not one of them dies. How is it possible the discovery of undef possibly successful? The reason I found this was because the guy I work with did this:

 open my $in, '>', $ARGV[0] or die q{couldn't open $ARGV[0]}; 

He thought that this would kill the script if no arguments were passed (I know that this is not the cleanest way to do this, but I did not think that this would not work).

I am using Strawberry 5.16.1.

+8
undefined file-io perl
source share
1 answer

See perldoc -f open :

As a special case, a form with three arguments with read / write mode and a third undef argument:

 open(my $tmp, "+>", undef) or die ... 

opens the file descriptor of an anonymous temporary file.

+10
source share

All Articles