Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^5: Sub Return Value Help

by JediWizard (Deacon)
on Aug 01, 2005 at 18:20 UTC ( [id://480002]=note: print w/replies, xml ) Need Help??


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

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

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

    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\

      I just belive in establishing "Best Practices". If it is better for one instance, and has no negative effect in any instance, why not make a habit of always doing it the "best" way? Granted there are a million instances in which a bareword filehandle will not hurt you... But there are as many instances in which putting a gun to your temple and pulling the trigger will not kill you... but that still doesn't mean you should do it.

      Update: Please enlighten me: when would it be "impossible" to use a lexical scalar over a bareword filehandle? If there is such a situation... I cannot imagine it.


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

      —Andy Warhol

        You appear to have completely reversed the sense of my last paragraph. I was saying completely the opposite.

        You are still saying that lexical filehandles are somehow "better" in all cases without providing any concrete reasons. I would agree that lexical filehandles are extremely useful in some circumstances, but I am not buying into this notion that bareword filehandles are somehow "bad practice" just because you have decided to assert this as fact.

        /J\

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-24 03:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found