Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

question about star character in Perl

by dicty (Sexton)
on Jan 17, 2013 at 23:25 UTC ( #1013913=perlquestion: print w/ replies, xml ) Need Help??
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

Comment on question about star character in Perl
Re: question about star character in Perl
by davido (Archbishop) 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

Re: question about star character in Perl
by kennethk (Monsignor) 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!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1013913]
Approved by Old_Gray_Bear
Front-paged by Lotus1
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (11)
As of 2014-10-22 08:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (114 votes), past polls