Why do I have to use a * in front of a Perl bareword filehandle?

后端 未结 3 1551
無奈伤痛
無奈伤痛 2020-12-16 03:05

While trying to do this:

 my $obj = new JavaScript::Minifier;
 $obj->minify(*STDIN, *STDOUT);
// modified above line to
 $obj->minify(*IP_HANDLE,*OP_HA         


        
3条回答
  •  攒了一身酷
    2020-12-16 03:48

    The * refers to a Perl "typeglob", which is an obscure implementation detail of Perl. Some older Perl code needs to refer to file handles using typeglobs (since there wasn't any other way to do it at the time). More modern code can use filehandle references instead, which are easier to work with.

    The * is analogous to $ or %, it refers to a different kind of object known by the same name.

    From the perldata documentation page:

    Perl uses an internal type called a typeglob to hold an entire symbol table entry. The type prefix of a typeglob is a * , because it represents all types. This used to be the preferred way to pass arrays and hashes by reference into a function, but now that we have real references, this is seldom needed.

提交回复
热议问题