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

I would like to be able to make this bit of code generate an error.

my $y; my @y;
I only confuse myself when I code this way, and I only do it accidentally. So I would like to say something like:
use strict 'confusingly_overloaded_variable_names';
Any suggestions for what I should learn so that I can implement this? Or has someone already done it, or tried and failed?

It should work perfectly the first time! - toma

Replies are listed 'Best First'.
Re: Confusing variable names
by BrowserUk (Patriarch) on May 04, 2003 at 23:08 UTC

    You might take a look at the module B::Lint which does similar sort of things, albeit as a static check rather than at runtime, but it might give you a feel for what would be involved.

    Getting offical approval to use one of the $^H bits (see S* strict.pm), and getting your code adopted in the core might be harder, but there would be nothing to stop you doing this for your own benefit.

    * Corrected case of S. Thanks Bart.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
Re: Confusing variable names
by allolex (Curate) on May 05, 2003 at 00:17 UTC

    Effective Perl Programming: Writing Better Programs with Perl has this to say about the issue:

    It's not necessarily bad programming style to take advantage of Perl's independent namespaces by giving two different kinds of variables the same name. Sometimes it even seems like the sensible thing to do:

    @who = grep { /\bjoebloe\b/ } `who`;
    @who contains output from who command, one line per element.
    foreach $who (@who) { # ... do something with $who }
    Iterate over each line of output using variable $who.

    --
    Allolex

Re: Confusing variable names
by toma (Vicar) on May 05, 2003 at 06:28 UTC
    Thanks to BrowserUK I got on the right track looking at the B modules. I decided to narrow the problem down to finding lexical variables in modules, since almost all my variables are of this sort. I didn't see how to handle lexical variables using the techniques in B::Lint, but I ran across B::LexInfo. This just about gets me what I want, although it hasn't been tested thoroughly:
    use B::LexInfo; use strict; # The name of the module to test goes in # the next two lines. my $module_name= "Data::Table"; use Data::Table; my $lexi = B::LexInfo->new; my $info = $lexi->stash_cvlexinfo($module_name); foreach my $pad (@{$info}) { foreach my $sub (keys %{$pad}) { my %lexhash; foreach my $next (keys %{$pad->{$sub}}) { if ($next =~ /(\$)(.*)/ or $next =~ /(@)(.*)/ or $next =~ /(%)(.*)/) { my ($op, $varname)= ($1,$2); if (exists $lexhash{$varname}) { print "$sub has lexicals $op$varname". " and $lexhash{$varname}$varname\n"; } else { $lexhash{$varname}= $op; } } } } }
    I could make this a module and add a little more B code to it, so that it would check a whole program and all the loaded modules. I could use it with something like:
    use VerifyLexicalsWontConfuseTomA;
    It should work perfectly the first time! - toma
      if you are willing...you could extend your code and make a "standards" module that could enforce coding guidelines for perl teams. this could check parantheses style, var name length, your var name pragma, etc. i don't understand how some perlmonks can get on a pedestal and preach to others that the open style is the only way--that's not the perl way at all. if "they" don't want to use this for their work, simply do not include this module. Individuality is a good thing, to a point...but on a team project, standards promote quality assurance. Chris
        As one of those pedestal-ian monks, I'll try and explain it.

        The issue is that every single Perl style guide I've ever seen has been a C or C++ style guide with funny characters thrown in. For example, using extra parentheses in C makes sense. The cases where the parens aren't needed or wanted are few and far between. However, adding parens to Perl everywhere you can makes it look like Lisp with a bad case of acne. (This is in the style guide for where I'm currently working. This is also the style guide everyone is ignoring.)

        Most companies either don't know they're running Perl, think Perl isn't worth the time, are actively trying to phase out all open-source-dependent projects, or consider Perl to be a bastardized version of C. This is even more prevalent in Perl-based dot-coms. (The style guide in that place was even worse than where I am right now with all the idiotic parentheses!)

        What I'm getting at is that standards are anything but, common sense isn't, and the rest of us are extremely wary of such a thing. This isn't to say that you shouldn't do it. In fact, if it works for you, I would love to help work on such a project! (It sounds neat-o keen. But, don't expect me to work within standards I didn't agree to... *grins*)

        ------
        We are the carpenters and bricklayers of the Information Age.

        Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

        Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: Confusing variable names
by vladb (Vicar) on May 04, 2003 at 22:47 UTC
    I hope I don't come out sounding critical ;-)

    ... but this is one thing you might have to get used to when coding in Perl. I don't think any additional 'pragma' or check would be necessary and you may very well make do with simple coding guidelines which would prohibit the use confusing variable names to begin with. It might take a while though for the rules to trickle to every member of your development team (if you are a part of one). Remember, some people consider this being one of the sexiest features of Perl and may buckle at changing their coding style to accommodate least experienced (from their point of view ;) Perl developers. ;-)

    _____________________
    # Under Construction
Re: Confusing variable names
by Abigail-II (Bishop) on May 04, 2003 at 23:09 UTC
    No, that's not present in Perl, and it's unlikely that ever will. There are warnings for certain things that might indicate you have made a mistake, for instance the use of unintialized values, or using a package variable only once.

    But no warnings for things that might confuse you. That's your own problem. Or maybe, perception. Because if you use both my $y and my @y, it wasn't confusing at the moment you wrote it. And since there's no possibility of actually confusing them - they remain quite distinct variables - Perl will not issue a warning.

    And I think it should stay that way. Compilers should warn if there's the possibility of a mistake, but they should not have an opinion about coding style.

    Abigail

Re: Confusing variable names
by toma (Vicar) on May 04, 2003 at 23:26 UTC
    Perhaps my question left people with the impression that I want to change perl. I do not. I just want to make a bit of code generate an error. Perhaps strict is not a good mechanism for this. Something like B::Lint may be better, I'll take a look at it.

    This is not a question about whether it is a good idea or not. It's my time and my code, so that part is up to me!

    It should work perfectly the first time! - toma

Re: Confusing variable names
by draconis (Scribe) on May 05, 2003 at 14:17 UTC
    This sort of thing used to bite me in the begining as well. After deep thought on the subject (because of being bitten - and my pre-disposition at the time to other languages) I came to the following conclusion.

    This is part of Perl's construct. In the Perl world - the difference is clear. Naming them similarly, and then using them interchangeablely or using them 'correctly' comes with time.

    When I need an array - out of habit now - I do not create a scalar by that name (as in my mind it is now reserved for use when dealing with the array). The converse is true for me as well.

    I personally, now that I am 'used' to this, find this feature of benefit as it lends itself to more streamlined coding and provides a built-in coding standard.

Re: Confusing variable names
by Your Mother (Archbishop) on May 05, 2003 at 18:24 UTC
    I liked a few of the replies a lot (++s around) and I'd never dispute the wisdom of leaving it to the coder. But I think something like this could be useful to others besides toma.

    I was trying to read some procedural Perl about a year ago that was written by a C coder making a dip into Perl for the expediency. It was almost 1_000 lines long and didn't contain more than one or two variables that were longer than one letter. There isn't enough espresso in Abyssinia to keep my focus that strong.

    This could be useful for keeping certain minimum style requirements in a tech group.