[comp.lang.perl] sort differs in PL#36 from PL#28?

ishii@hexard.co.jp (Koji Ishii) (10/22/90)

  sort in a packge is different.  In PL#28,

hxrdgw:~/unnews/test[21]% perl28 sorttest.pl
1 5 4 9
1 4 5 9(28)
1 5 4 9(36)

But in PL#36

hxrdgw:~/unnews/test[22]% perl sorttest.pl
1 5 4 9
1 5 4 9(28)
1 4 5 9(36)

Is this a feature or ???  sorttest.pl follows.

#!/usr/bin/perl
&sorttest( 1, 5, 4, 9 );
exit 0;

package test;

sub numerical28 { $main'a - $main'b; }
sub numerical36 { $a - $b; }

sub main'sorttest {
	local( @x ) = @_;
	print "@x\n";
	local( @y ) = sort numerical28 @x;
	print "@y(28)\n";
	local( @z ) = sort numerical36 @x;
	print "@z(36)\n";
}
--
----------------------------------
   Koji Ishii  Hexard Inc., Tokyo, Japan.     JUNET: ishii@hexard.co.jp

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (10/23/90)

In article <ISHII.90Oct22104324@hxrdgw.hexard.co.jp> ishii@hexard.co.jp (Koji Ishii) writes:
: 
:   sort in a packge is different.  In PL#28,
: 
: hxrdgw:~/unnews/test[21]% perl28 sorttest.pl
: 1 5 4 9
: 1 4 5 9(28)
: 1 5 4 9(36)
: 
: But in PL#36
: 
: hxrdgw:~/unnews/test[22]% perl sorttest.pl
: 1 5 4 9
: 1 5 4 9(28)
: 1 4 5 9(36)
: 
: Is this a feature or ???  sorttest.pl follows.
: 
: #!/usr/bin/perl
: &sorttest( 1, 5, 4, 9 );
: exit 0;
: 
: package test;
: 
: sub numerical28 { $main'a - $main'b; }
: sub numerical36 { $a - $b; }
: 
: sub main'sorttest {
: 	local( @x ) = @_;
: 	print "@x\n";
: 	local( @y ) = sort numerical28 @x;
: 	print "@y(28)\n";
: 	local( @z ) = sort numerical36 @x;
: 	print "@z(36)\n";
: }

This is part of what was meant when the patch said "package behavior is
now more consistent".  Essentially, packages used to be strictly lexical
with certain exceptions such as eval and double-quoted strings.  Messy.
Now the package of the currently executing statement is known at run-time,
so variables are automatically created in the current package without
having to make exceptions.  Thus, $a and $b above are automatically
package local variables, which is what you'd expect.  I'm sufficiently
into having Perl do what you expect that I thought it worthwhile to
possibly break a few scripts to make things consistent.  I kind of feel
that this is my last chance to make such adjustments before the book comes
out.

Larry

ishii@hexard.co.jp (Koji Ishii) (10/24/90)

In article <10063@jpl-devvax.JPL.NASA.GOV>
	lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) writes:

> In article <ISHII.90Oct22104324@hxrdgw.hexard.co.jp>
>	ishii@hexard.co.jp (Koji Ishii) writes:
> : 
> :   sort in a packge is different.  In PL#28,
		:
> This is part of what was meant when the patch said "package behavior is
> now more consistent".  Essentially, packages used to be strictly lexical
> with certain exceptions such as eval and double-quoted strings.  Messy.
> Now the package of the currently executing statement is known at run-time,
> so variables are automatically created in the current package without
> having to make exceptions.
		:
> Larry

  Thank you, it is a feature.  So follows is the only way to sort in
numerical order with both PL#28 and PL#36, isn't it?  All other ways I
tested failed but the following one.  Are there better way to do this?

#!/usr/bin/perl
&sorttest( 1, 5, 4, 9 );

package test;

sub numerical28 { $main'a - $main'b; }
sub numerical36 { $a - $b; }

sub main'sorttest {
	local( $numfunc ) = "numerical36";
	$numfunc = "numerical28" if $] <= 3.028;
	print "numfunc = '$numfunc'\n";
	local( @x ) = sort $numfunc @_;
	print "@x\n";
}
--
----------------------------------
   Koji Ishii  Hexard Inc., Tokyo, Japan.     JUNET: ishii@hexard.co.jp