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
Get Chitika Premium
Posted by J. Gleixner on February 29, 2008, 5:41 pm
Please log in for more thread options
szr wrote:
> 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?

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.

Posted by Ben Morrow on February 29, 2008, 5:53 pm
Please log in for more thread options

> Joost Diepenmaat wrote:
> >
> > I would think:
<snip>
> >
> > 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.
>
> 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


Posted by szr on February 29, 2008, 8:30 pm
Please log in for more thread options
Ben Morrow wrote:
>> Joost Diepenmaat wrote:
>>>
>>> I would think:
> <snip>
>>>
>>> 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.
>>
>> 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.

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

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