in reply to Text::ExtractWords exhibits incomprehensible behavior?

  ibm1620 wrote: I even tried making a copy of $text (my $copy = $text) and passing $copy to words_list(), and afterwards *both* $copy and $text were munged.

That I don't understand. Were $text and $copy munged in the same way after the call or different? What does printf("%s %s\n", \$text, \$copy) give before and after the call?

In general, it seems like a bad practice for an XS function to modify it's arguments unless that is the specific purpose.

  • Comment on Re: Text::ExtractWords exhibits incomprehensible behavior?

Replies are listed 'Best First'.
Re^2: Text::ExtractWords exhibits incomprehensible behavior?
by ibm1620 (Hermit) on Jun 16, 2024 at 21:39 UTC
    Yes, exact same way, despite being at different addresses:
    #!/usr/bin/env perl use v5.38; use Text::ExtractWords qw(words_list); say $^V; my $text = "12/21/84 Bob's 21st b'day was a wine-and-dine."; my $copy = $text; say "Text: $text"; say "Copy: $copy"; printf("%s %s\n", \$text, \$copy); my @list; words_list(\@list, $copy, {minwordlen => 2, maxwordlen => 26 }); say "Found words: " . join ' ', map {"[$_]"} @list; say "Text: $text"; say "Copy: $copy"; printf("%s %s\n", \$text, \$copy); __END__ v5.40.0 Text: 12/21/84 Bob's 21st b'day was a wine-and-dine. Copy: 12/21/84 Bob's 21st b'day was a wine-and-dine. SCALAR(0x140829a68) SCALAR(0x1408299d8) Found words: [12] [21] [84] [bob's] [21st] [b'day] [was] [a] [wine-and +-dine] Text: 122184bob's21stb'daywasawine-and-dine Copy: 122184bob's21stb'daywasawine-and-dine SCALAR(0x140829a68) SCALAR(0x1408299d8)
      That's the SV addresses that are different. Try Devel::Peek's Dump function to see what the PV is? I suspect this is supposed to be copy-on-write (COW), but that the XS code is just overwriting the (still-shared) data area without doing the correct COW API stuff.
        That must be it.
        v5.40.0 Text: 12/21/84 Bob's 21st b'day was a wine-and-dine. Copy: 12/21/84 Bob's 21st b'day was a wine-and-dine. SV = PV(0x15780b090) at 0x1578242d0 REFCNT = 1 FLAGS = (POK,IsCOW,pPOK) PV = 0x6000019a1fe0 "12/21/84 Bob's 21st b'day was a wine-and-dine." +\0 CUR = 46 LEN = 48 COW_REFCNT = 2 SV = PV(0x15780b0b0) at 0x157824258 REFCNT = 1 FLAGS = (POK,IsCOW,pPOK) PV = 0x6000019a1fe0 "12/21/84 Bob's 21st b'day was a wine-and-dine." +\0 CUR = 46 LEN = 48 COW_REFCNT = 2 Found words: [12] [21] [84] [bob's] [21st] [b'day] [was] [a] [wine-and +-dine] Text: 122184bob's21stb'daywasawine-and-dine Copy: 122184bob's21stb'daywasawine-and-dine SV = PV(0x15780b090) at 0x1578242d0 REFCNT = 1 FLAGS = (POK,IsCOW,pPOK) PV = 0x6000019a1fe0 "12\x0021\x0084\x00bob's\x0021st\x00b'day\x00was +\x00a\x00wine-and-dine\x00"\0 CUR = 46 LEN = 48 COW_REFCNT = 2 SV = PV(0x15780b0b0) at 0x157824258 REFCNT = 1 FLAGS = (POK,IsCOW,pPOK) PV = 0x6000019a1fe0 "12\x0021\x0084\x00bob's\x0021st\x00b'day\x00was +\x00a\x00wine-and-dine\x00"\0 CUR = 46 LEN = 48 COW_REFCNT = 2