On Aug 26, 12:25 pm, shlo...@shlomifish.org (Shlomi Fish) wrote:
> On Fri, 26 Aug 2011 20:08:31 +0100
>
> ...
>
> The problem starts to happen when you try to declare $a and $b using my. This
> program:
>
> [CODE]
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my $a = 5;
> my $b = 6;
>
> print map { "$_\n" } sort { $a <=> $b } (9,100,5,6,70,3,4,98,28,27);
> [/CODE]
>
> Yields this error:
>
> [CODE]
> Can't use "my $a" in sort comparison at test.pl line 9.
> [/CODE]
>
> This is perl-5.14.1 - previous versions of Perl may behave more
> erratically when executing this. $a and $b are built-ins plain and simple, and
> should be treated as such, due to their use by perldoc -f sort and other
> functions from List::Util, List::MoreUtils, etc.
>
> Another reason not to use them except for those cases is because "a" and "b"
> are not very meaningful and indicative identifiers.
>

Their use elsewhere should definitely ring a cautionary
bell.  Here  'use diagnostics qw/-verbose/  explains the
problem well:

   erbose diagnostic



> > Overly-careful warnings can have the opposite of the desired effect,
> > especially on beginner programmers, and make it seem like the language
> > is rife with pitfalls and gotchas, especially when these apply to
> > ubiquitous core concepts like $_. I hope people will think twice about
> > the ideas that they are conveying.
>
> > Cheers all,
>
> > Rob
>
> --
> -----------------------------------------------------------------
> Shlomi Fish      http://www.shlomifish.org/
> Why I Love Perl -http://shlom.in/joy-of-perl
>
> Sophie: Let’s suppose you have a table with 2^n cups…
> Jack: Wait a second! Is ‘n’ a natural number?
>
> Please reply to list if it's a mailing list post -http://shlom.in/reply.


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to