Using PadWalker in a overload sub turned out to be problematic. The sub that overloads tr's operands cannot access the content of $x in such situation:

{ my $x = 'abc; my $s = 'edcba'; eval '$s =~ tr/$x/123/'; } { my $x = 'ABC'; }

peek_my(1)->{'$x'} then gives \undef. Though I could work around this, I abandoned this approach and came to something similar: A generator for a sub, that can be used like tr/$x/$y/$opt without eval'ing $x and $y.

After I put this together I realized that something similar already exists: Regexp::Tr. However, the tr-arguments are eval'ed there.

Here is demo with all parts in a single file:

#!/usr/bin/perl use strict; use warnings; ### Internal only package String::Compile::Tr::Overload; use overload; sub _ovl_tr { our ($search, $replace); return $_[1] unless $_[2] eq 'tr'; return "$search" if $_[1] eq ':search:'; return "$replace" if $_[1] eq ':replace:'; $_[1]; } sub import { overload::constant q => \&_ovl_tr; } sub unimport { overload::remove_constant q => \&_ovl_tr; } ### The module itself package String::Compile::Tr; *search = *String::Compile::Tr::Overload::search; *replace = *String::Compile::Tr::Overload::replace; BEGIN { String::Compile::Tr::Overload->import; } sub trgen { local our ($search, $replace); my $options; ($search, $replace, $options) = @_; $options = '' unless defined $options; my ($opt) = $options =~ /^([cdsr]*)$/; $opt = '' unless defined $opt; my $template = <<'EOS'; sub { local *_ = \$_[0] if @_; tr/:search:/:replace:/%s; } EOS my $code = sprintf $template, $opt; eval $code; } BEGIN { String::Compile::Tr::Overload->unimport; } ### main package main; use feature 'say'; *trgen = *String::Compile::Tr::trgen; my $x = 'abc'; my $y = '123'; # compile a sub my $tr = trgen($x, $y); my $str = 'fedcba'; # like $str =~ tr/abc/123/: $tr->($str); say $str; # fed321 my @list = qw(axy bxy cxy); # like tr/abc/123/ $tr->() for @list; say "(@list)"; # (1xy 2xy 3xy);

Greetings,
🐻

$gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$

In reply to Re: Overloading tr/// operands by jo37
in thread Overloading tr/// operands by jo37

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.