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

Hi

I have to tunnel extra information (about creation) to a string passed to a third party module.

The module expects a string and I have an API to later retrieve that string.

Two approaches come to mind:

Before reinventing the wheel, are there already older (or other) solutions for that?

Cheers Rolf
(addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re: piggy back infos on a string scalar
by haj (Vicar) on Feb 23, 2023 at 18:26 UTC
    String::Tagged allows to attach data to (arbitrary parts of a) string, the objects "behave like strings". This is a bit of over-engineering if you need to attach only to the string as a whole, but alas, it's already existing!
      Thanks, very interesting,

      I'm working on something roughly similar...

      Cheers Rolf
      (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
      Wikisyntax for the Monastery

Re: piggy back infos on a string scalar
by kikuchiyo (Hermit) on Feb 23, 2023 at 22:35 UTC

    There is perhaps Variable::Magic? It even has an example "useful for passing user data through limited APIs".

    Caveat: "Magic is not copied on assignment." So whether this technique works for you depends on what your third party module does with the string.

      Thanks, I already tested the overload approach and it works fine ... at least for the time being.

      The third party module is DBI and it's the statement string with attached debug infos.

      When inspecting $sth->{Statement} I can see the original object while $sth = $dbh->prepare($stmt_obj) processed the stringification.

      (Alas it's a bit more complicated since DBI depends on driver implementations which may differ, I already stumbled over things which are different for MySQL than documented, so testing and adjusting might be needed.)

      Cheers Rolf
      (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
      Wikisyntax for the Monastery

Re: piggy back infos on a string scalar
by tobyink (Canon) on Feb 24, 2023 at 16:04 UTC

    Use object overloading.

    Ties and Variable::Magic (which was mentioned elsewhere in the thread) attach to the variable, not to the value, like this:

    my $x = "Hello world";
    
    attach_some_special_magic( $x );
    
    my $y = $x;
    
    # $y has no special magic
    

    As you're passing the string around, you don't want that. You want an overloaded object.

    The danger with overloaded objects is that the third-party module you mention might check !ref( $str ) to ensure it's been passed a string. If it's doing that, you still have one workaround. Bless the object into a package called "0". Then ref( $str ) will return "0" which is false.

      > Bless the object into a package called "0". Then ref( $str ) will return "0" which is false.

      LOL. I think the "0" namespace could become quite overloaded now ;-)

      Cheers Rolf
      (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
      Wikisyntax for the Monastery