Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

How to find barewords in perl code?

by mithaldu (Monk)
on Jun 01, 2010 at 14:57 UTC ( #842630=perlquestion: print w/replies, xml ) Need Help??
mithaldu has asked for the wisdom of the Perl Monks concerning the following question:

I am currently rewriting a rather large code-base that makes extensive and terrifying use of bareword filehandles. In order to clean it up i intend to replace them all with proper FileHandle objects. However quite a few of them have very imaginative names like "G".

So, in order to make this task a bit more sane to complete: Is there some kind of module i can run the code through which parses it and can tell me where barewords are?

Replies are listed 'Best First'.
Re: How to find barewords in perl code?
by moritz (Cardinal) on Jun 01, 2010 at 14:58 UTC
    I'm pretty sure that Perl::Critic can be used to detect bareword filehandles, it probably even has a pre-defined rule for that.
      It does! Thanks a lot.
Re: How to find barewords in perl code?
by JavaFan (Canon) on Jun 01, 2010 at 16:02 UTC
    And if it wasn't for perlcritic, I'd think that /(?:open(?:dir)?|print|close|flock|readdir)\s+[A-Za-z] would find most of them, with little false positives. (Of course, one can easily construct false positives and false negatives).
      I'd add a (?![,(]) to the end of that regex to exclude print functioncall() and print functionall, otherstuff.

      Now let the bikeshedding and micro-optimizations begin!

        Hmmm, forgot about function calls like that. For some reason, I assumed perl would warn. But even this is warning free:
        sub foo {shift} sub bar {1} print foo bar;
        and just prints 1.

        OTOH, in pre-5.6 days, when I was still using bare word file handles, I used all uppercase names, and I use all lowercase for subs, so /(?:print|...)\s+[A-Z]/ would have worked for my code.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://842630]
Approved by moritz
Front-paged by moritz
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2018-05-20 22:30 GMT
Find Nodes?
    Voting Booth?