develooper Front page | perl.perl5.porters | Postings from September 2018

[perl #133505] open() dies for invalid modes, instead of returningundef, as documented

Thread Previous
From:
James E Keenan via RT
Date:
September 8, 2018 14:28
Subject:
[perl #133505] open() dies for invalid modes, instead of returningundef, as documented
Message ID:
rt-4.0.24-31213-1536416869-1131.133505-15-0@perl.org
On Sat, 08 Sep 2018 11:18:12 GMT, rdiezmail-perl@yahoo.de wrote:
> 
> 
> The following code:
> 
> 
> 
> my $memFileContents;
> 
> 
> open( my $memFile, 'ERROR_HERE', \$memFileContents )
>             or die "Cannot create in-memory file: $!\n";
> 
> 
> yields this error message:
> 
> 
> Unknown open() mode 'ERROR_HERE' at main.pl line 8.
> 
> 
> The error message seems like a run-time error, and not a compilation
> error. However, the documentation states: "Open returns nonzero on
> success, the undefined value otherwise." Therefore, open() should not
> die itself, but return undef, and hopefully also set $! accordingly.
> 

I believe your interpretation of the documentation for 'open' (pod/perlfunc.pod) is incorrect.  The discussion in that document of open's return value *comes after* the discussion of the two- and three-argument versions of open in which the valid modes for open are described.  So it presumes that someone understands those nine paragraphs.  Given that, it shouldn't be surprising that you get what is indeed a run-time error when you use open with an invalid mode.

A correctly formulated open call will indeed return an undefined value when it cannot open a file:

#####
$ ls -l foo.txt
ls: cannot access 'foo.txt': No such file or directory

$ perl -e 'my $rv = open my $IN, q|<|, q|foo.txt|; defined $rv ? print "$rv\n" : print "not defined\n";'
not defined
#####

And a correctly formulated open call will return a non-zero value when it succeeds:

#####
$ echo "This is a test" > 133505.txt

$ perl -e 'my $rv = open my $IN, q|<|, q|133505.txt|; print "$rv\n";'
1
#####

Thank you very much.

-- 
James E Keenan (jkeenan@cpan.org)

---
via perlbug:  queue: perl5 status: new
https://rt.perl.org/Ticket/Display.html?id=133505

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About