Closed p5pRT closed 15 years ago
When the iteration operator \<> is overloaded with
use overload '\<>' => 'method';
the invoked method does not recognize when it is invoked from list context\, i.e. wantarray always returns FALSE. If the method is invoked immediately\, without overloading\, it recognizes its list environment.
I added a small package (Count.pm) to demonstrate the problem. It implements a class that counts from 0 to 9\, overloading the \<> operator to method iterate. The following oneliners should all produce the same output '0123456789'\, but the 4th\, where the \<> is called in list context only produces '0' (1\, 2 and 3 are OK).
1% perl -MCount -wle 'my $x=new Count;print while $x->iterate' 2% perl -MCount -wle 'my $x=new Count;print for $x->iterate' 3% perl -MCount -wle 'my $x=new Count;print while \<$x>' 4% perl -MCount -wle 'my $x=new Count;print for \<$x>'
Hope this is enough information...
Regards\, Arjen Bax -- CMG Noord-Nederland B.V. Sector Telecommunications & Utilities Postbus 70237\, 9704 AE Groningen Tel. (050)52 19 500\, Fax (050)52 19 504 \<http://www.cmg.nl> \<http://www.cmg.com>
Adding manpower to a late software project makes it later.
When the iteration operator \<> is overloaded with
use overload '\<>' => 'method';
the invoked method does not recognize when it is invoked from list context\, i.e. wantarray always returns FALSE. If the method is invoked immediately\, without overloading\, it recognizes its list environment.
I added a small package (Count.pm) to demonstrate the problem. It implements a class that counts from 0 to 9\, overloading the \<> operator to method iterate. The following oneliners should all produce the same output '0123456789'\, but the 4th\, where the \<> is called in list context only produces '0' (1\, 2 and 3 are OK).
1% perl -MCount -wle 'my $x=new Count;print while $x->iterate' 2% perl -MCount -wle 'my $x=new Count;print for $x->iterate' 3% perl -MCount -wle 'my $x=new Count;print while \<$x>' 4% perl -MCount -wle 'my $x=new Count;print for \<$x>'
There was a discussion about that in 1999 and Ilya said that the context shouldn't be recognized in overloaded operators.
The discussion can be found here: http://markmail.org/message/ffd6sunhu6mfchfz
When the iteration operator \<> is overloaded with
use overload '\<>' => 'method';
the invoked method does not recognize when it is invoked from list context\, i.e. wantarray always returns FALSE. If the method is invoked immediately\, without overloading\, it recognizes its list environment.
I added a small package (Count.pm) to demonstrate the problem. It implements a class that counts from 0 to 9\, overloading the \<> operator to method iterate. The following oneliners should all produce the same output '0123456789'\, but the 4th\, where the \<> is called in list context only produces '0' (1\, 2 and 3 are OK).
1% perl -MCount -wle 'my $x=new Count;print while $x->iterate' 2% perl -MCount -wle 'my $x=new Count;print for $x->iterate' 3% perl -MCount -wle 'my $x=new Count;print while \<$x>' 4% perl -MCount -wle 'my $x=new Count;print for \<$x>'
There was a discussion about that in 1999 and Ilya said that the context shouldn't be recognized in overloaded operators.
The discussion can be found here: http://markmail.org/message/ffd6sunhu6mfchfz
module@renee-baecker.de - Status changed from 'open' to 'resolved'
When the iteration operator \<> is overloaded with
use overload '\<>' => 'method';
the invoked method does not recognize when it is invoked from list context\, i.e. wantarray always returns FALSE. If the method is invoked immediately\, without overloading\, it recognizes its list environment.
I added a small package (Count.pm) to demonstrate the problem. It implements a class that counts from 0 to 9\, overloading the \<> operator to method iterate. The following oneliners should all produce the same output '0123456789'\, but the 4th\, where the \<> is called in list context only produces '0' (1\, 2 and 3 are OK).
1% perl -MCount -wle 'my $x=new Count;print while $x->iterate' 2% perl -MCount -wle 'my $x=new Count;print for $x->iterate' 3% perl -MCount -wle 'my $x=new Count;print while \<$x>' 4% perl -MCount -wle 'my $x=new Count;print for \<$x>'
There was a discussion about that in 1999 and Ilya said that the context shouldn't be recognized in overloaded operators.
The discussion can be found here: http://markmail.org/message/ffd6sunhu6mfchfz
When the iteration operator \<> is overloaded with
use overload '\<>' => 'method';
the invoked method does not recognize when it is invoked from list context\, i.e. wantarray always returns FALSE. If the method is invoked immediately\, without overloading\, it recognizes its list environment.
I added a small package (Count.pm) to demonstrate the problem. It implements a class that counts from 0 to 9\, overloading the \<> operator to method iterate. The following oneliners should all produce the same output '0123456789'\, but the 4th\, where the \<> is called in list context only produces '0' (1\, 2 and 3 are OK).
1% perl -MCount -wle 'my $x=new Count;print while $x->iterate' 2% perl -MCount -wle 'my $x=new Count;print for $x->iterate' 3% perl -MCount -wle 'my $x=new Count;print while \<$x>' 4% perl -MCount -wle 'my $x=new Count;print for \<$x>'
There was a discussion about that in 1999 and Ilya said that the context shouldn't be recognized in overloaded operators.
The discussion can be found here: http://markmail.org/message/ffd6sunhu6mfchfz
2008/11/17 reneeb via RT \perlbug\-comment@​perl\.org:
When the iteration operator \<> is overloaded with
use overload '\<>' => 'method';
the invoked method does not recognize when it is invoked from list context\, i.e. wantarray always returns FALSE. If the method is invoked immediately\, without overloading\, it recognizes its list environment.
I added a small package (Count.pm) to demonstrate the problem. It implements a class that counts from 0 to 9\, overloading the \<> operator to method iterate. The following oneliners should all produce the same output '0123456789'\, but the 4th\, where the \<> is called in list context only produces '0' (1\, 2 and 3 are OK).
1% perl -MCount -wle 'my $x=new Count;print while $x->iterate' 2% perl -MCount -wle 'my $x=new Count;print for $x->iterate' 3% perl -MCount -wle 'my $x=new Count;print while \<$x>' 4% perl -MCount -wle 'my $x=new Count;print for \<$x>'
There was a discussion about that in 1999 and Ilya said that the context shouldn't be recognized in overloaded operators.
The discussion can be found here: http://markmail.org/message/ffd6sunhu6mfchfz
The only thing is that discussion doesn't really address the \<> operator. All of that discussion addressed binary operators\, and if it did address the \<> operator i think it would have been clear that it is actually needed in at least that case\, and any other operator that can return lists.
cheers\, Yves
-- perl -Mre=debug -e "/just|another|perl|hacker/"
On Tue\, 18 Nov 2008 12:43:41 +0100 demerphq \demerphq@​gmail\.com wrote:
2008/11/17 reneeb via RT \perlbug\-comment@​perl\.org:
When the iteration operator \<> is overloaded with
use overload '\<>' => 'method';
the invoked method does not recognize when it is invoked from list context\, i.e. wantarray always returns FALSE. If the method is invoked immediately\, without overloading\, it recognizes its list environment.
I added a small package (Count.pm) to demonstrate the problem. It implements a class that counts from 0 to 9\, overloading the \<> operator to method iterate. The following oneliners should all produce the same output '0123456789'\, but the 4th\, where the \<> is called in list context only produces '0' (1\, 2 and 3 are OK).
1% perl -MCount -wle 'my $x=new Count;print while $x->iterate' 2% perl -MCount -wle 'my $x=new Count;print for $x->iterate' 3% perl -MCount -wle 'my $x=new Count;print while \<$x>' 4% perl -MCount -wle 'my $x=new Count;print for \<$x>'
Well\, you can try this:
package Count; sub TIEHANDLE { my $count = 0; return bless sub { return () if $count > 9; return wantarray ? ($count .. 9) : $count ++ ; } => (shift() || __PACKAGE__); } *iterate = *READLINE = sub { goto &{ $_[0] } }; sub new { require Carp; Carp::croak("I'm fit to be tied"); } 1;
And then
% perl -MCount -wle 'tie local *CTR => "Count"; for (1..10) { print \
% perl -MCount -wle 'tie local *CTR => "Count"; for (1..10) { print scalar \
% perl -MCount -wle 'my $x=tie local *CTR => "Count"; for (1..10) { print $x->iterate }' Name "main::CTR" used only once: possible typo at -e line 1. 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789
% perl -MCount -wle 'my $x=tie local *CTR => "Count"; for (1..10) { print scalar $x->iterate }' Name "main::CTR" used only once: possible typo at -e line 1. 0 1 2 3 4 5 6 7 8 9
% perl -MCount -wle 'tie local *CTR => "Count"; for (1..10) { print (tied(*CTR)->iterate) }' 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789
There was a discussion about that in 1999 and Ilya said that the context shouldn't be recognized in overloaded operators.
The discussion can be found here: http://markmail.org/message/ffd6sunhu6mfchfz
The only thing is that discussion doesn't really address the \<> operator. All of that discussion addressed binary operators\, and if it did address the \<> operator i think it would have been clear that it is actually needed in at least that case\, and any other operator that can return lists.
Apart from =\, those scant few operators that *can* return lists (provided they've been used in list context) are only:
0: + print +localtime; # gag me w/a spoon 1: x @trips = (@a) x 3; # but not x= 2: \, @c = (@a\, @b); 3: .. @trey = 1.2..3.4; # (1\,2\,3): of course! 3: .. @nums = (0 .. 9); 4: ... @nums = (0 ... 9); # same IFF list context 5: && "true" && @a # but not &&= 6: || @a || "whatever" # but not ||= 7: // undef // localtime() # but not //= 8: // print /[aeiouy]/i && //g; # prints all vowels\, or none 8: // @many_codons = /.../g; 9: \\ @refrefs = \\localtime; # mirabile visu 9: \ @numrefs = \localtime; # nearly so 10: ?: @max = (@a > @b) ? @a : @b; 11: ?? @once_codons = ?...?g; # highlander match 12: \<> @stuff = \; # readline() 13: \<> use lib \<~/stdlib>; # glob() 13: \<> @zones = \<America/{Chicago\,Denver\,Phoenix\,Los_Angeles}>; 14: -> @fils = threads->list; # double-entendre 15: => use constant MEALS => # how to feed a hobbit qw(
Coffee
Breakfast
Second-Breakfast
Brunch
Lunch
Munch
Tea/Tapas
Dinner
Supper
Snacker
Snooker
);
And arguably:
16: @{} @ans = @{ f("x") }; 17: %{} @ans = %{ f("x") }; 18: &{} @ans = &{ f("x") };
I shan't visit named listops like sort\, map\, grep\, or reverse\, nor the sillyops like or and and or and and or.
--tom
Migrated from rt.perl.org#6748 (status was 'resolved')
Searchable as RT6748$