in reply to Re^2: Sub Return Value Help
in thread Sub Return Value Help

Bare work file handles are not as safe as lexically scoped scalars. Just because they were used a decade ago doesn't mean we should continue to use them when a more robust alternative is available. Walking everywhere one needed to go worked fine for centuries. Does that mean we shouldn't use cars? What used to be the standard should not always continue. Change can be good. Before perl 5, we used typeglobs to pass around data structures: If you recomend that... you are insane.


They say that time changes things, but you actually have to change them yourself.

—Andy Warhol

Replies are listed 'Best First'.
Re^4: Sub Return Value Help
by gellyfish (Monsignor) on Aug 01, 2005 at 17:58 UTC

    Please explain why you believe bareword filehandles are not as safe, and that lexical filehandles are more robust. I can see occasions where the latter might be preferable but I wouldn't necessarily be making an out of hand assertion for all applications.

    /J\

      Bareword file handles are always global. Any thrid party, or even first party code, called while the filehandle is open can close, redirect it to a differnt file, seek to a different position, or whatever it wants. With a lexically scoped scalar you (the person writing the code to use it) has fulll controll. Using a bareword is like leaving your car unlocked... Sure no one should do anything with it, but they can if they want.


      They say that time changes things, but you actually have to change them yourself.

      —Andy Warhol

        But I still don't see why this will be a problem in *all* cases, or in most likelihood the case in question in particular - a single file program which is unlikely to be used in a shared environment: there is no opportunity for any unwanted access to the filehandles.

        It should also be borne in mind that whilst the bareword filehandle might be globally accessible, the symbols are package qualified as demonstrated by the following:

        use strict; use warnings; + open FOO, ">x"; + package bar; + close FOO or warn "$!\n";; + package main; + print FOO "Gshshsh";
        It would therefore be reasonable to assume the risk of unintentional access is fairly limited, leaving only a notional risk of intentional misuse of the filehandle by some malicious code - and, well, if you are running untrusted code in a manner that would allow that to happen then you have far worse problems than messing with a programs file handles would create.

        As I say, lexical filehandles have their uses when it would be tricky or impossible to use bareword filehandles. But I just don't go with the absolutism.

        /J\