Magic $a $b

A

Achim Peters

Hi,

I'm a noob to perl and a frequent reader of this group. Whenever anyone
in here uses $a and/or $b in a non-sort context s/he gets a "Don't!
They're magic." as a response. Now, I believe in magic and the wisdom of
regulars and that's why I mentioned this general rule in
de.comp.lang.perl.misc.

But there happened to be a non-believer and he dared me with "Oh,
really? Perl this:"

use strict;
use warnings;

$a = 4711;

my @arr = qw /x a u q r/;
@arr = sort { $a cmp $b } @arr;

print "$a\n";

which in fact does print the 4711 (a German "magic" number) with Perl
5.8.2, thus no side effects of the sorting (except of course that the
missing "my" in front of $a does not cause an compile error.

Is there any example to prove his impertinence of questioning the gurus'
wisdom?

TIA

Bye
Achim
 
P

Peter Makholm

Achim Peters said:
But there happened to be a non-believer and he dared me with "Oh,
really? Perl this:"

use strict;
use warnings;

$a = 4711;

See mom, no warning! - That's magic.

Using $a and $b defies parts of of using strict.

//Makholm
 
J

Joost Diepenmaat

Achim Peters said:
which in fact does print the 4711 (a German "magic" number) with Perl
5.8.2, thus no side effects of the sorting (except of course that the
missing "my" in front of $a does not cause an compile error.

Is there any example to prove his impertinence of questioning the gurus'
wisdom?

I would think:

sub srt {
my @arr = qw /x a u q r/;
@arr = sort { $a cmp $b } @arr;
}

srt();
my $a = 4711;
print "$a\n";

versus:

srt();
my $a = 4711;
print "$a\n";

sub srt {
my @arr = qw /x a u q r/;
@arr = sort { $a cmp $b } @arr;
}

would be enough reason not to use $a and $b.
 
S

szr

Joost said:
I would think:

sub srt {
my @arr = qw /x a u q r/;
@arr = sort { $a cmp $b } @arr;
}

srt();
my $a = 4711;
print "$a\n";

versus:

srt();
my $a = 4711;
print "$a\n";

sub srt {
my @arr = qw /x a u q r/;
@arr = sort { $a cmp $b } @arr;
}

would be enough reason not to use $a and $b.

When I try the latter, I get the following, using Perl 5.8.8:

Can't use "my $a" in sort comparison at line 9.

0001: #!/usr/local/bin/perl -w
0002: my @arr = srt();
0003: my $a = 4711;
0004: print "$a\n";
0005: print join (', ', @arr);
0006:
0007: sub srt {
0008: my @arr = qw /x a u q r/;
0009: @arr = sort { $a cmp $b } @arr;
0010: }


The error message itself seems to be wrong, or more percisely, the wrong
error for the given situation. There is no "my" in the "sort { ... }"
clause. Is this be a bug?

Furthur, why does it generate an error in the first place?
 
J

J. Gleixner

szr said:
When I try the latter, I get the following, using Perl 5.8.8:

Can't use "my $a" in sort comparison at line 9.

0001: #!/usr/local/bin/perl -w
0002: my @arr = srt();
0003: my $a = 4711;
0004: print "$a\n";
0005: print join (', ', @arr);
0006:
0007: sub srt {
0008: my @arr = qw /x a u q r/;
0009: @arr = sort { $a cmp $b } @arr;
0010: }


The error message itself seems to be wrong, or more percisely, the wrong
error for the given situation. There is no "my" in the "sort { ... }"
clause. Is this be a bug?

Furthur, why does it generate an error in the first place?

Are you kidding??????????

You're supposed to get an error. That's the reason Joost
posted the very concise example - to show the OP why
it might be bad to use $a.
 
B

Ben Morrow

Quoth "szr said:
When I try the latter, I get the following, using Perl 5.8.8:

Can't use "my $a" in sort comparison at line 9.

The error message itself seems to be wrong, or more percisely, the wrong
error for the given situation. There is no "my" in the "sort { ... }"
clause. Is this be a bug?

No, it's not. The point is that the $a in scope at line 9 is the 'my $a'
from five lines earlier, rather that the global $main::a in scope at the
start of the program, and sort (for various nasty implementation-specfic
reasons) can't use a lexical $a. This is exactly why you shouldn't use
$a other than for sort.

Ben
 
S

szr

Ben said:
No, it's not. The point is that the $a in scope at line 9 is the 'my
$a' from five lines earlier, rather that the global $main::a in scope
at the start of the program, and sort (for various nasty
implementation-specfic reasons) can't use a lexical $a. This is
exactly why you shouldn't use $a other than for sort.

Ah. Its very clear now. Thank you (and J. Gleixner) I'm nto sure why I
didn't catch that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top