In my situation I don't need warnings Use of uninitialized value in string while comparing string equality. So I tought that instead silencing all such warnings in the scope with no warnings 'uninitialized' would be better to overload eq-operator with my own subroutine, like:
use overload 'eq' => \&undefined_equal;
sub undefined_equal {
my ( $left, $right ) = @_;
no warnings 'uninitialized';
if ( $left eq $right ) {
return 1;
}
return 0;
}
Of course, overloading does not work, because according to the docs, overload is meant to use with classes, but I have plain procedural packages.
So I did try with overloading built-in functions, like:
package REeq;
use strict; use warnings; use 5.014;
BEGIN {
use Exporter ();
@REeq::ISA = qw( Exporter );
@REeq::EXPORT = qw( eq );
}
sub eq {
my ( $left, $right ) = @_;
no warnings 'uninitialized';
if ( $left CORE::eq $right ) {
return 1;
}
return 0;
}
1;
I can call my eq but can't use it as operator.
I need it because I want instead
if ( defined $some_scalar && $some_scalar eq 'literal string' ){
....
}
to use just
if ( $some_scalar eq 'literal string' ){
....
}
How could I achieve my goal?