Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

use strict won't require explicit name for all variables?

by Biker (Priest)
on Feb 28, 2002 at 10:08 UTC ( [id://148176]=perlquestion: print w/replies, xml ) Need Help??

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

A co-worker in my shop is starting to appreciate the smell of Perl. We had a discussion about recursive function calls in general and he went off writing a small test script to get familiar with the concept.

I told him to use strict and warnings or die. He didn't want to die just yet, so he obeyed. Then he came to me asking for an explanation to something I hadn't foreseen:

#!/usr/bin/perl -w use strict; my $line; open (IN, "<david2"); open (OUT, ">temp"); while ($line = <IN>) { process($line); } exit; sub process { my $str=shift; # my $rest; ($a, $b, $rest) = split(/\s+/,$str,3); if (defined($a)){ print OUT "$a\t"; } if (defined($b)){ print OUT "$b\n"; } if (!defined($rest)){ return (0); } process($rest); }
If he runs the above script, perl will complain that $rest requires explicit package name etc. Fine. Removing the comment before my $rest will make perl happy. Fine again.

The question is: Why doesn't perl require explicit package names for $a and $b?

We have tried the script both under Solaris and Win-32 Active State, Perl 5.6 in both cases. Same result.


Everything will go worng!

Replies are listed 'Best First'.
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.
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.

      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!

        Well, its not really a problem, so im not sure Perl 6 will do much about it.

        ;-)

        Yves / DeMerphq
        --
        When to use Prototypes?

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.

    /=\
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?
$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>

      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!

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?"
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
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!
      Presumably it was a decision to optimize the common case. Most programmers wont name variables things like $a and $b just cause single letter variables are bad practice. (Although $i, $j, $k, and $x, $y, $z have a looong tradition of being used in computer programming and mathematics so generally speaking they are exempted from the "use descriptive variable names" advice) Otoh _some_ name was needed for the variables that would be used by sort, and since they would probably be used a lot, and since a good descriptive short name for them isnt exactly obvious I think the decision was the right one.

      --- demerphq
      my friends call me, usually because I'm late....

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?
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.

      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!

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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-23 21:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found