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

When executed
use Audio::TagLib; $test = shift; $file = Audio::TagLib::FileRef->new($test); say 'title:', $file->tag()->title()->toCString(); say 'track', $file->tag()->track()->toCString();
Perl says
title:Title Test Can't locate object method "toCString" via package "0" (perhaps you fo +rgot to load "0"?) at ./taglib_test.pl line 9.
Note that the two calls are identical, except for the title/track. Similar calls in a different context don't have the problem.

My question: How can I discover what Perl sees that results in the error? Many thanks. (FWIW the module reports content from a MP3 file)

Replies are listed 'Best First'.
Re: Unusual "Can't locate object method"
by choroba (Cardinal) on Oct 31, 2024 at 22:04 UTC
    It seems $file->tag->title returns a Audio::TagLib::String object, while $file->tag->track returns a plain number.

    How do I know?

    say 'title:', ref $file->tag->title; # Audio::TagLib::String say 'track:', ref $file->tag->track; # Nothing.

    BTW: It also seems the CPAN module Audio::TagLib doesn't correctly declare Path::Class as its dependency.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: Unusual "Can't locate object method"
by sectokia (Friar) on Oct 31, 2024 at 22:23 UTC

    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

    .
      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".)

        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(θ)| |
Re: Unusual "Can't locate object method"
by Anonymous Monk on Oct 31, 2024 at 21:58 UTC
    the source says track is just an int, why do you expect it to have a toCString method?