sakoh@sraco2.us.sra.co.jp (Hiroshi &) (04/09/91)
What's wrong with this program?
#!/usr/bin/perl
sub as_numeric { $a <=> $b; }
package other;
@c = (2,3,2,6,4,2);
@d = sort main'as_numeric @c;
for $e (@d) {
print "$e\n";
}
I got following output which I didn't expect.
2
3
2
6
4
2
I tested it on SONY-NEWS (OS3.3) and NeXTstation (2.0) with 3.44, 4.0beta, 4.0.
--
sakoh@sra.co.jp
"Whereof one cannot speak, thereof one must remain silent." ---Wittgenstein
"Sometimes noise is significant." ---William Hewlett
tchrist@convex.COM (Tom Christiansen) (04/09/91)
From the keyboard of sakoh@sraco2.us.sra.co.jp (Hiroshi &): :What's wrong with this program? : : #!/usr/bin/perl : sub as_numeric { $a <=> $b; } : : package other; : : @c = (2,3,2,6,4,2); : @d = sort main'as_numeric @c; : : for $e (@d) { : print "$e\n"; : } What's wrong is that $a and $b are compiled in the main routine, but are being passed in as $other'a and $other'b. Solutions are: 1) move the subroutine declaration below the package statement. 2) reference the vars with their $other' qualification. 3) pull out the package with caller and use an eval to get it right. perl -w points out the problem, albeit non-obviously. --tom