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

I've got a pretty good grasp of typeglobs and filehandles but one thing I don't get is the difference between:
my $fh = *STDOUT; and my $fh = \*STDOUT;
The first one places a filehandle directly into the contents of $fh while the second puts a reference to a filehandle into it. I'm with you there. But what's the practical difference between the two assignments? Is there ever a reason for using one over the other?

For example, the line below works with both cases above:

print $fh "What the?";
Similarly, the open function works just as well with either kind of indirect filehandle assignment. Thanks again, Monks. I'll make my donation in advance.

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop";
$nysus = $PM . $MCF;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re: Indirect filehandles
by dragonchild (Archbishop) on Jul 17, 2001 at 21:51 UTC
    While I don't have an answer to your question, I do have a question for you - why are you using the typeglob to begin with? I would, instead, do something like:

    use FileHandle; my $out = new FileHandle ">&STDOUT"; autoflush $out; my $in = new FileHandle "<&STDIN";

    That way, you don't have any typeglob confusion.

    Now, the method above doesn't work in Win32 the way it does in Unix, cause you cannot select on $in. (Of course, I have a sneaking suspicion that you cannot select on STDIN, no matter how you try to do it, but that's neither here nor there...)

      Yes, I know there is usually little need nowadays for this kind of indirect filehandle. My question is to just satisfy my curiousity. I did find out that the assigment as a reference is blessable. See How can I use a filehandle indirectly?. But I think there has to be some other difference.

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop";
      $nysus = $PM . $MCF;
      Click here if you love Perl Monks

Re: Indirect filehandles
by petral (Curate) on Jul 17, 2001 at 23:20 UTC
    Well, if you had read the documentation... :-). Actually, I'm pretty sure I've seen "perl automaticall converts *glob to a reference" (or maybe only *glob{IO}) somewhere, but I can't find it right now.
    update Ok, 2 immediate downvotes, means, I guess, that the signal/noise nazis need some scraps:
    "*foo{IO} is an alternative to the \*HANDLE mechanism" -- perlman perlref.
    But there is a more explicit one somewhere, maybe someone else will remember seeing it recently and where.  At all events, nysus's example shows that this is so.

      p