Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Passing filehandles to subs.

by r.joseph (Hermit)
on May 05, 2001 at 12:16 UTC ( #78202=perlquestion: print w/ replies, xml ) Need Help??
r.joseph has asked for the wisdom of the Perl Monks concerning the following question:

Really quick question. When passing around filehandles between subs, what is the difference between:
sub one (*) { my $fh = shift; print $fh "$fh (glob)"; } open (OUTER,"+>out.txt") or die "$!\n\n"; one (\*OUTER);
and a very slight change:
sub one (*) { my $fh = shift; print $fh "$fh (glob)"; } open (OUTER,"+>out.txt") or die "$!\n\n"; one (*OUTER);
As you can see, in the first example it seems that I have passed a glob reference to the sub, while in the second example I have simply passed the glob. Both work exactly the same, yet I have seen both - what is the difference (is one advantageous over the other)?

Also, what is the reason that if I protoype one() like this:
sub one(\*) { ... }
it doesn't work, but
sub one(*) { ... }
does? I would think that using \* would just require a glob reference - where am I wrong?

Thanks for the help - these glob things are very new to me!

r. j o s e p h
"Violence is a last resort of the incompetent" - Salvor Hardin, Foundation by Issac Asimov

Comment on Passing filehandles to subs.
Select or Download Code
Re: Passing filehandles to subs.
by Chady (Priest) on May 05, 2001 at 12:25 UTC

    In the first example you're 'passing a reference to the corresponding typeglob. and this is the best way to pass filehandles to or from subroutines, since it has the optical effect of removing the ambiguity of the typeglob'

    As for the prototyping question: 'a backslash quoted character signifies that the argument absolutely must start with that character - for example, \@ would require that the function call specify a list as the first argument'.. but then you are specifying a typeglob which is in fact everything.. so you're requiring 'everything' to be the first argument, which is pointless...

    the quoted lines are from a book... and I hope it helps a bit.

    Update: fixed some typos


    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/
Re (tilly) 1: Passing filehandles to subs.
by tilly (Archbishop) on May 05, 2001 at 16:03 UTC
    An incidental piece of advice.

    First go out and read this explanation of what prototypes are in Perl.

    Then drop them from your code entirely. It will be much improved for it...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2014-08-23 00:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (168 votes), past polls