http://www.perlmonks.org?node_id=1013913

dicty has asked for the wisdom of the Perl Monks concerning the following question:

I have a question about "*" character in Perl. In the code below, what does "*" or "*XML" mean?

open (XML, "<$xml_file");

get_data(*XML, \%hash);

Thanks a lot!

Dicty

Replies are listed 'Best First'.
Re: question about star character in Perl
by kennethk (Abbot) on Jan 17, 2013 at 23:41 UTC
    The * sigil refers to Typeglobs and Filehandles. In this case, the author wanted to pass the newly-opened XML filehandle into a subroutine, and old fashioned Perl didn't have a particularly clean way to pass one. Nowadays, you'd probably write that as:
    open (my $XML, "<", $xml_file) or die "File open failed ($xml_file): +$!; get_data($XML, \%hash);

    The indirect filehandle makes passing and stashing much cleaner (IMHO). See also open and Indirect Filehandles in perlopentut.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Got it. Thanks a lot!
Re: question about star character in Perl
by davido (Cardinal) on Jan 17, 2013 at 23:38 UTC

    The '*' character in this context is a sigil for typeglobs. It is to typeglobs approximately as '$' is to scalars, '%' is to hashes, '@' is to arrays, and '&' is to subroutines.

    See perldoc perldata | Typeglobs and Filehandles.

    In this case 'XML' is a bareword filehandle. It is being passed as a typeglob to the get_data() function, which presumably is populating %hash with a dump of the XML file.


    Dave