Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: question about: my $a AND sort {$a <=> $b} keys %hash

by Discipulus (Canon)
on Aug 06, 2015 at 09:13 UTC ( [id://1137632]=note: print w/replies, xml ) Need Help??


in reply to question about: my $a AND sort {$a <=> $b} keys %hash

you must receive an error even without warnings enabled:
Can't use "my $a" in sort comparison at pmonks06082015.pl line 4.
In short, never use $a and $b in your programs. It is explicitly mentioned in perlvar
$a $b Special package variables when using sort(), see sort. Because of this + specialness $a and $b don't need to be declared (using use vars , or + our()) even when using the strict 'vars' pragma. Don't lexicalize th +em with my $a or my $b if you want to be able to use them in the sort +() comparison block or function.


L*
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^2: question about: my $a AND sort {$a <=> $b} keys %hash
by Laurent_R (Canon) on Aug 06, 2015 at 09:29 UTC
    Don't lexicalize them with my $a or my $b if you want to be able to us +e them in the sort() comparison block or function.
    But, on the other hand you can localize them when needed:
    $ perl -we ' local $a = 1; @h = sort {$a <=> $b} keys %hash; print "@h\n";> %hash = map { $_ , undef } 0 .. 3; @h = sort {$a <=> $b} keys %hash; print "@h\n"; ' 0 1 2 3

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-19 08:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found