Click here to get back home

Magic $a $b

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Magic $a $b Achim Peters 02-28-2008
|--> Re: Magic $a $b Peter Makholm02-28-2008
---> Re: Magic $a $b Joost Diepenmaa...02-28-2008
Posted by Achim Peters on February 28, 2008, 5:59 am
Please log in for more thread options
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

Posted by Peter Makholm on February 28, 2008, 6:04 am
Please log in for more thread options

> 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

Posted by Joost Diepenmaat on February 28, 2008, 6:08 am
Please log in for more thread options

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

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

Posted by Achim Peters on February 29, 2008, 7:29 am
Please log in for more thread options
Joost Diepenmaat schrieb:
>
>> 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:

> my $a = 4711;
[...]

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

Tnx! He converted and now is a true fellow believer. ;-)

Bye
Achim

Posted by szr on February 29, 2008, 5:27 pm
Please log in for more thread options
Joost Diepenmaat wrote:
>
>> 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.

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?

--
szr



Similar ThreadsPosted
Q: // and "magic" April 5, 2005, 2:59 pm
$| (undocumented) magic? September 10, 2004, 12:15 am
where is my magic (XS related) March 1, 2005, 11:01 am
Magic <> makes STDIN unusable - How to fix? April 15, 2005, 9:23 pm
Magic for object constructor wanted January 31, 2008, 1:05 am
setting perlio layers (e.g., utf8) on "magic" ARGV file handle June 10, 2005, 10:18 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap