develooper Front page | perl.perl5.porters | Postings from January 2012

Re: [perl #109146] 3-arg open of undef for read

From:
Ævar Arnfjörð Bjarmason
Date:
January 27, 2012 01:58
Subject:
Re: [perl #109146] 3-arg open of undef for read
Message ID:
CACBZZX5+UD5c7eQOjFN0pyzc=-B+3QXK+NsRzxs37OkcOs7XpQ@mail.gmail.com
On Fri, Jan 27, 2012 at 02:25, Yitzchak Scott-Thoennes
<perlbug-followup@perl.org> wrote:

> open(FH, "<", undef) triggers special behavior to open a temporary file
> (at least with perlio) that is documented as only applying to +> and +<.
>
> To keep open(FH, "<", $hash{typoedkey}) from quietly succeeding, this
> should at least trigger a warning (and the doc be updated to not be mode
> specific).
>
> Crude patch from avar in irc:
>
> diff --git a/perlio.c b/perlio.c
> index a985dcc..09e415d 100644
> --- a/perlio.c
> +++ b/perlio.c
> @@ -1601,6 +1601,8 @@ PerlIO_openn(pTHX_ const char *layers, const
> char *mode, int fd,
>  {
>     dVAR;
>     if (!f && narg == 1 && *args == &PL_sv_undef) {
> +       if (mode && strEQ(mode, "r"))
> +           Perl_croak(aTHX_ "You have an undef filename with a < mode");
>        if ((f = PerlIO_tmpfile())) {
>            if (!layers || !*layers)
>                layers = Perl_PerlIO_context_layers(aTHX_ mode);
>

Two points:

 * That patch is probably in completely the wrong place, it should
   likely be in Perl_pp_open or Perl_do_openn.

 * With that patch all of perl's tests pass, which means that we have
   no tests for this feature.

And also:

 * Did this just slip between the cracks when in-memory file support
   was added (i.e. open my $fh, ">", \(my $file))? Shouldn't we always
   be dying when passed undef with modes that aren't +> or <+ ? From
   perldoc -f open:

        As a special case the three-argument form with a
        read/write mode and the third argument being "undef":

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

        opens a filehandle to an anonymous temporary file.
        Also using "+<" works for symmetry, but you really
        should consider writing something to the temporary file
        first.  You will need to seek() to do the reading.

   Is this used for anything else?

 * What do we actually get on `open(FH, "<", undef)`? It seems to me
   that we create some skeleton filehandle that we can't actually do
   anything with.



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