Hi, I haven't being using perl for too long. Can someone explain the
correct way to get the sort function to recognize an anonymous function
declared as a hash value? Look at my sample code for clarification:
#!/usr/bin/perl
@my_array = qw( g a z f u q m i e b );
$hash{my_sort_s ub} = sub { $a cmp $b };
$hash{test_rout ine} = sub { print "test_routi ne works\n" };
&{$hash{test_ro utine}};
print @my_array;
print "\n";
print( sort &{$hash{my_sort _sub}} @my_array );
print "\n";
The code fails to compile with error:
Array found where operator expected at ./test.pl line 9, near "} "
(Missing operator before ?)
syntax error at ./test.pl line 9, near "} @my_array "
Execution of ./test.pl aborted due to compilation errors.
Help! I don't see what's wrong with this.
correct way to get the sort function to recognize an anonymous function
declared as a hash value? Look at my sample code for clarification:
#!/usr/bin/perl
@my_array = qw( g a z f u q m i e b );
$hash{my_sort_s ub} = sub { $a cmp $b };
$hash{test_rout ine} = sub { print "test_routi ne works\n" };
&{$hash{test_ro utine}};
print @my_array;
print "\n";
print( sort &{$hash{my_sort _sub}} @my_array );
print "\n";
The code fails to compile with error:
Array found where operator expected at ./test.pl line 9, near "} "
(Missing operator before ?)
syntax error at ./test.pl line 9, near "} @my_array "
Execution of ./test.pl aborted due to compilation errors.
Help! I don't see what's wrong with this.
Comment