Re: use strict won't require explicit name for all variables?
by Matts (Deacon) on Feb 28, 2002 at 10:15 UTC
|
$a and $b are special variables in Perl. See perldoc -f sort. | [reply] |
Re: use strict won't require explicit name for all variables?
by dws (Chancellor) on Feb 28, 2002 at 10:18 UTC
|
The question is: Why doesn't perl require explicit package names for $a and $b?
Change $a and $b to $x and $y, and you'll get the error. This is probably an artifact of the semi-special nature $a and $b in sort blocks.
| [reply] |
|
Thanks go out to both dws and Matts. I've never been using the sort routines in Perl. And I never name my variables like $a, $b or $c either. So I haven't been in this trouble.
I hope that perl 6 will do something to avoid this 'problem'. In the mean time, I'll tell my co-worker to avoid those names. ;-)
Everything will go worng!
| [reply] |
|
| [reply] |
|
|
|
Re: use strict won't require explicit name for all variables?
by ViceRaid (Chaplain) on Feb 28, 2002 at 10:19 UTC
|
I suspect that it's because $a and $b are, in some places, variables with a "special" meaning to Perl. I'm thinking particularly of sort, which uses $a and $b to stand for the values being compared. If I change the script to use, say $x and $y, I get a strict error, as I'd expect.
This would make $a and $b a bit like the punctuation variables. $_, for example, doesn't need to be explicitly scoped before you use it, which is partly why it's useful as a shortcut.
/=\
| [reply] |
Re: use strict won't require explicit name for all variables?
by smitz (Chaplain) on Feb 28, 2002 at 12:59 UTC
|
The smell of perl???
use Scratch::N::Sniff
Give me two weeks, itl'l be on CPAN. Volunteers?
| [reply] [d/l] |
$a and $b are bad variable names anyway
by petdance (Parson) on Feb 28, 2002 at 14:30 UTC
|
And really, you shouldn't be using $a and $b as variable names anyway. Surely there is something more descriptive than $a and $b (or $field1 and $field2, or whatever) that shows the meaning of what those variables do.
xoxo,
Andy
--
<megaphone>
Throw down the gun and tiara and come out of the float!
</megaphone>
| [reply] |
|
I fullheartedly agree and I have told my co-worker so. As I mentioned in my earlier node, I wouldn't use such variable names at all.
But the point was merely why we couldn't use the variable names as any variable name. Just imagine that in this specific case it did have a real meaning. (Which it didn't.)
Everything will go worng!
| [reply] |
Re: use strict won't require explicit name for all variables?
by thunders (Priest) on Feb 28, 2002 at 17:19 UTC
|
$a and $b in a sort will not overwrite a global or block-level $a or $b. $a and $b are local to the sort. So this is not a concern. Forgetting to properly scope $a and $b is a bigger concern. I guess it would be hard to misspell them, so scratch that off the list of drawbacks to this "feature" :-)
Just keep the behavior of $a and $b in mind, or better don't use them as variable names, cause there's always something more descriptive than a one letter name.
As an aside when I first read about sorts I was like "yeah but where are $a and $b defined?"
| [reply] |
Re: use strict won't require explicit name for all variables?
by webfiend (Vicar) on Feb 28, 2002 at 17:29 UTC
|
Just a quick editorial comment on $a and $b. Since so many of Perl's internal variables are indicated by Funny Characters(TM), having plain old letters for the sort variables may not quite sink in right away. It hasn't bit me in any of my own code (my problem is one of excessively verbose names rather than overly terse), but I can see how it might confuse somebody who is looking for special variables with names like $_ or $!.
Now that I think about it... I've never had any problems remembering about the regex variables $1, $2, etcetera. The context makes it quite clear what purpose they serve. Guess I just have to remember that, like so many things in Perl, $a and $b are just a question of context.
"All you need is ignorance and confidence; then success is sure."-- Mark Twain
| [reply] [d/l] [select] |
Re: use strict won't require explicit name for all variables?
by Anonymous Monk on Feb 28, 2002 at 21:31 UTC
|
It is funny to see people telling Biker not to use var names
like $a and $b, they should pass that advice on to Larry Wall or
who ever decided to use $a and $b with sort! | [reply] |
|
| [reply] |
Re: use strict won't require explicit name for all variables?
by rbc (Curate) on Feb 28, 2002 at 17:13 UTC
|
Why not also mention the file handles IN and OUT aren't
they variables too? | [reply] |
Re: use strict won't require explicit name for all variables?
by Dog and Pony (Priest) on Feb 28, 2002 at 19:13 UTC
|
The reasons for this is already thouroughly explained, so I just want to add a word of "wisdom"... or something.
In any programming generally, you should really use describing variables, because, much like "use strict", it will save you a lot of grief down the road. In perl, specifically, it also makes sure you don't clash with any special variables. Most of the time, people just don't name their variables $" or $\, even when not being verbose, so it is rarely a problem - apart from a few cases like this one.
I do understand that this was only test code, but it doesn't hurt to use names like $first_element and
$second_element or something, anyways. And, use lowercase variable names, so it will never clash with for example the names from "use English".
You have moved into a dark place.
It is pitch black. You are likely to be eaten by a grue. | [reply] [d/l] [select] |
|
OK. Agreed. 100%.
But I can't stop myself from thinking that there might or will be situations where $a and $b may be meaningful.
In a Windows specific application, when accessing diskette drives A: and B: it wouldn't be very far fetched to name two variables $a and $b. If I already know that it's a bad thing I wouldn't do it. But someone who doesn't know about the special meaning of $a and $b could find it quite natural to use those variable names in this situation.
Personally, I have today learnt about $a and $b for life. But if I could be unaware of their special meaning, many more people will. My co-worker, who definitively is a Perl beginner, had no way of understanding the behavior on his own.
Everything will go worng!
| [reply] |
Re: use strict won't require explicit name for all variables?
by Anonymous Monk on Feb 28, 2002 at 17:37 UTC
|
I don't know why. I suspect it is something in the strict module.
$a & $b can be uses without declaring even when strict is in use. But, if you use $c throught $z you will get an error for not delcaring with my.
paulc.
| [reply] |