Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

why don't filehandles have punctuation before their name?

by Anonymous Monk
on Apr 24, 2001 at 03:12 UTC ( #74908=perlquestion: print w/replies, xml ) Need Help??

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

In perl, scalars start with $, arrays start with @, typeglobs start with *, and hashes start with %. Why don't filehandles have a punctuation character before their name?
  • Comment on why don't filehandles have punctuation before their name?

Replies are listed 'Best First'.
Re: why don't filehandles have punctuation before their name?
by Yohimbe (Pilgrim) on Apr 24, 2001 at 03:19 UTC
    They can have punctuation
    use FileHandle; $fh = new FileHandle; if ($fh->open("< file")) { print <$fh>; $fh->close; }
    File handles are just a scalar, IIRC, and the uppercasing is just a convention, a holdover from the other languages from whence it came.
    --
    Jay "Yohimbe" Thorne, alpha geek for UserFriendly
      Actually, filehandles (and dirhandles) are not just scalars. In fact, if you look at the value returned by new FileHandle, you will see that it is a reference to a GLOB.

      Because filehandles (and dirhandles) don't have a special syntax, like scalars/arrays/hashes, you have to refer to the entire glob instead. This is why you see code like:

      sub myprint { my($fh) = @_; print $fh "Hello world.\n"; } myprint \*STDOUT;
      The FileHandle and IO::Handle modules make it more convenient to use arbitrary handles, because they hide the reference-to-glob syntax.
      Filehandles are uppercased so that the introduction of new perl keywords (which are always lowercased) do not break existing Perl scripts. Larry has endorsed this convention.

      It seems to me, however, that new keywords could still break existing scripts if those scripts included subroutines with the same names as the keywords. So to be consistent we ought to name our subroutines in all uppercase as well, or at least throw in a few uppercase letters in our subroutine names.

        Perhaps you didn't notice the addition of BEGIN, END, CHECK, DESTROY, and INIT, etc. (: In fact, I think you should name subroutines with mixed case and should avoid the bareword-as-filehandle syntax. The bareword-as-filehandle syntax caused lots of problem even way back in Perl4. It is nice to have an alternative to it in Perl5.

                - tye (but my friends call me "Tye")
        So to be consistent we ought to name our subroutines in all uppercase as well, or at least throw in a few uppercase letters in our subroutine names.
        I prefer to include at least one _. As far as I know, no built-in functions contain underscores, and probably never will.
Re: why don't filehandles have punctuation before their name?
by turnstep (Parson) on Apr 24, 2001 at 04:12 UTC
    They could have....and don't forget directory handles as well. Some say that Larry just ran out of symbols...
Re: why don't filehandles have punctuation before their name?
by clintp (Curate) on Apr 24, 2001 at 04:34 UTC
    Consider the fact that in older dialects of perl (Perl 4 and such) that the parser can generally guess when something is supposed to be a filehandle and when it's not. (First argument of open, first argument to flock, close, etc...).

    (Remember too that functions used to have mandatory ()'s and &'s! So the barewords weren't so ambiguous then...)

    It only begins to get tricky and nasty when you have indirect filehandles and use <> for globbing.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2021-12-09 08:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (36 votes). Check out past polls.

    Notices?