Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Puzzled by value of $overload::ops{binary}

by syphilis (Archbishop)
on Jun 24, 2024 at 12:48 UTC ( [id://11160189]=perlquestion: print w/replies, xml ) Need Help??

syphilis has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

On Windows (perl 5.40.0 and 5.38.0 at least):
>perl -Moverload -le "print $overload::ops{binary};" & &= | |= ^ ^= &. &.= |. |.= ^. ^.=
I am familiar with the first 6 operators, but the last 6 operators (ie the ones containing a ".") are not something I'm familiar with.
For example, what would one expect $obj1 &. $obj2 to do ?
Or is it just something that I can use to do whatever I want ?

Are there examples somewhere (maybe in some CPAN module) of subs called by the overloading of those non-existent operators ?
Is there some documentation about them ?

It's odd that I haven't spotted them before - but tonight is the first time I've ever laid eyes on them.


Replies are listed 'Best First'.
Re: Puzzled by value of $overload::ops{binary}
by ikegami (Patriarch) on Jun 24, 2024 at 13:13 UTC
      They're documented in perlop.

      Aaah ... one needs to use feature 'bitwise' to legitimize them.
      I don't use features - hence my lack of familiarity with these particular ops.
      Not an excuse but, as I should have searched perlop anyway.
      Thank you !!.

Re: Puzzled by value of $overload::ops{binary}
by cavac (Parson) on Jun 24, 2024 at 13:59 UTC

    but the last 6 operators (ie the ones containing a ".") are not something I'm familiar with

    Uhm, good question. Can't remember seeing these in the wild before. But when dealing with objects, you nearly always have to write your own operators overloads.

    And now for some fun:

    Or is it just something that I can use to do whatever I want ?

    As with pretty much every other thing involving the perl parser, the answer is a resounding yes. Mostly thanks to XS and source code filters, the perl interpreter is not limited to a single programming language, syntax, basic concept or even probably the somewhat limited charset available in Unicode. Only your imagination is the limit. The following may or may not be stuff that the Perl core devs intended, but which nevertheless is possible:

    #!/usr/bin/env perl use Acme::Brainfuck; print "Just another "; ++++++[>++++++++++++++++<-]> ++.-- >+++[<++++++>-]<.>[-]+++[<------>-]< +.- +++++++++.--------- ++++++++++++++.-------------- ++++++.------ >+++[<+++++++>-]<.>[-]+++[<------->-]< +++.--- +++++++++++.----------- print " hacker.\n";

    use JavaScript::Embedded; ## create new js context my $js = JavaScript::Embedded->new(); # set function to be used from javascript land $js->set('write' => sub { print $_[0], "\n"; }); $js->eval(qq{ (function(){ for (var i = 0; i < 100; i++){ write(i); } })(); });

    use Lingua::Shakespeare; The Infamous Hello World Program. Romeo, a young man with a remarkable patience. Juliet, a likewise young woman of remarkable grace. Ophelia, a remarkable woman much in dispute with Hamlet. Hamlet, the flatterer of Andersen Insulting A/S. Act I: Hamlet's insults and flattery. Scene I: The insulting of Romeo. [Enter Hamlet and Romeo] Hamlet: You lying stupid fatherless big smelly half-witted coward! You are as stupid as the difference between a handsome rich brave hero and thyself! Speak your mind! etc...

    use Acme::ButFirst; # Print a greeting, but first find caffiene. { print "Good morning!\n"; } but first { print "I need a coffee\n"; }

    use Inline ASM => <<'END', PROTOTYPES => {JAxH => 'void(char*)'}; .globl JAxH .text # prototype: void JAxH(char *x); JAxH: pushl %ebp movl %esp,%ebp movl 8(%ebp),%eax pushl %eax pushl $jaxhstr call printf movl %ebp,%esp popl %ebp ret .data jaxhstr: .string "Just Another %s Hacker\n" END print JAxH('Perl');

    And if you want run a hip AI company without actually writing a lot of code(), you can just recycle all the open sourced python stuff within Perl to woo your investors...

    print "9 + 16 = ", add(9, 16), "\n"; print "9 - 16 = ", subtract(9, 16), "\n"; use Inline Python => <<'END_OF_PYTHON_CODE'; def add(x,y): return x + y def subtract(x,y): return x - y END_OF_PYTHON_CODE

    () Writing actual code for a startup and producing value for the investors is just a waste of money. Money that could be much better spent on Booze, Babes and epic music festivals in the Bahamas featuring the latest in Instagram models...

      As with pretty much every other thing involving the perl parser, the answer is a resounding yes

      It seems that there are often limitations regarding the liberties that can be taken regarding overloading.
      I have overloading subs (XSubs) that will overload '++' as:
      void overload_inc($V * object, SV * second, SV * switch) { /* code that adds 1 to the value held by object */ }
      That always works perfectly well.
      I've then considered that I should be able to do the same thing with the overloading of the '+=' operator:
      void overload_plus_eq(SV * object, SV * addon, SV * switch) { /*code that adds the value of addon to the value held by object */ }
      That attempt (in overload_plus_eq), to modify the object in place always fails when called by the '+=' operator - and I have never been able to write that XSub such that it is successfully called via overloading of '+=', unless it returns a new object that overwrites the existing one.
      (Which, BTW, I think is what happens with the overloading of '+=' when only '+' is overloaded.)

      I've assumed the incapacity of '+=' overloading to "modify in place" is related to '+=' NOT being seen as a "mutator" by
      But maybe it's just that I haven't found the right way of doing it.

      I've often wished that overloading of "log2" and "log10" was available ... but not to the extent that I'm going to commandeer a couple of these 'bitwise' operators for that purpose ;-)
      (I doubt that could readily be achievable, anyway.)

      I've noticed that recognizes these additional bitwise operators as being valid, even though"feature 'bitwise'" has not been loaded:
      D:\>perl -Mwarnings -Moverload -e "use overload 'fu' => sub {return 1} +;" overload arg 'fu' is invalid at -e line 1. D:\>perl -Mwarnings -Moverload -e "use overload '&.' => sub {return 1} +;" D:\>perl -Mwarnings -Moverload -e "use overload '&.=' => sub {return 1 +};" D:\>
      Seems a bit odd .... but perhaps of little or no importance.

        I'd suggest PDL is the ultimate operator-overloader "source of truth". Mutating overloads is implemented in PDL::Ops, specifically (
        $ret .= pp_line_numbers(__LINE__, <<EOF) if $mutator; BEGIN { # in1, in2, out, swap if true \$OVERLOADS{'$op='} = sub { PDL::$name(\$_[0]->inplace, \$_[1]); \$_[0 +] }; } EOF
        Modifying the generated code for += to this:
        #line 192 "ops.pd" BEGIN { # in1, in2, out, swap if true $OVERLOADS{'+='} = sub { use Carp; use Test::More; diag "RUN ", Test::More::explain \@_; PDL::plus($_[0]->inplace, $_[1]); $_[0] }; }
        and running it like this:
        $ perl -MPDL -e '$p=pdl(1); $p+=2'
        # RUN [ # bless( do{\(my $o = '94749946365136')}, 'PDL' ), # 2, # undef # ]
        so your XS function should work. Did you make an overload for +=?

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11160189]
Front-paged by Corion
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-07-18 04:11 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.