in reply to overloading ">>" generates warning?

The warning has nothing to do with overloading. You can reproduce it with the core >>:

% perl -we '$foo = 1; 3 >> $foo' Useless use of right bitshift (>>) in void context at -e line 1.
As the warning says, you are using the operator in a void context. If you use it in a non-void context the error goes away; this is true even for your overloaded operator.

use strict; use warnings; package Foo; use overload '>>' => 'right_shift'; sub new {bless {}, __PACKAGE__} sub right_shift { print "OK!\n"} package main; my $foo = Foo->new; my $x = "test" >> $foo; __END__ OK!

The root of the problem is that you are trying to turn an operator that normally has no side effects into one that does (and, apparently it is only the side effects of this new operator that you're interested in). (In particular, note that the core >> does not modify its LHS.)

I suppose you can silence the warnings with something like

() = "test" >> $foo;

the lowliest monk

Replies are listed 'Best First'.
Re^2: overloading ">>" generates warning?
by Anonymous Monk on Aug 08, 2005 at 12:02 UTC
    what I'm trying to do is a file IO similar to IO::All' s, but only for files. It also uses this syntax, but don't generate any warnings. I didn't get how Ingy did that thing without a warning :(

      Use the source, Luke! Have you looked at the source code? :-)

      my $old_warn_handler = $SIG{__WARN__}; $SIG{__WARN__} = sub { if ($_[0] !~ /^Useless use of .+ \(.+\) in void context/) { goto &$old_warn_handler if $old_warn_handler; warn(@_); } };

      It looks like he basically turned off that warning globally.

      the lowliest monk

        ah... you're right :p funny thing is I did read the overload part, but didn't see the signal part :)