in reply to Unusual "Can't locate object method"

The cpan module for 'Audio::TagLib' doesn't have any useful documentation, but it just links to https://taglib.org/, under the idea that the API is the same for all programming languages.

If you go there and look under the API Reference for the Taglib::Tag class (https://taglib.org/api/classTagLib_1_1Tag.html) you will see that method title() returns a 'virtual string' (ie. another Taglib class), but method track() just returns an int.

Perl is telling you that that '0' (a scalar) has no method 'toCstring'. This indicates that track() has returned scalar value '0' (an integer), so you can't call a method on it. In comparison, Title() returns a so called 'taglib::string' class object which has methods as detailed here: https://taglib.org/api/classTagLib_1_1String.html

.

Replies are listed 'Best First'.
Re^2: Unusual "Can't locate object method"
by geoffleach (Scribe) on Nov 01, 2024 at 02:45 UTC
    Thanks for the reply's.

    I mention that the title() sub works as expected in other contexts. I should have made that more plain.

      Yes, and the title() method also works as expected in this context, which is why your output includes "title:Title Test".

      Your error is coming from the following line, where it calls track()->toCString() because, as others have stated, track() returns a plain integer, not an object, and plain integers don't have object methods. Because they're not objects.

      Change that line to say 'track', $file->tag()->track(); and it will do what you presumably want it to. (Namely, it will print "track0".)

        There are none so blind as those who will not ... read the code carefully.

        Thanks for helping me to see the obvious.

      Right, the title call succeeds and the title is output. Then the track toCString call fails because track() returns an integer, not an object that you can call a method on.
      --
      A math joke: r = | |csc(θ)|+|sec(θ)| |-| |csc(θ)|-|sec(θ)| |