in reply to OGG Vorbis tag parsing in pure perl

Prototypes in Perl are not what you think they are. Leave them out unless you know why you should. Not to mention they're entirely useless here because there is (and can be) no prototype checking on method calls. You can also condense the code significantly.
package gnump3d::oggtagreader; # untested sub new { bless \(undef), shift } sub get_tags { my($self, $filename) = @_; # using lexical filehandles is much safer # die()ing here is probably not the best way to handle failures he +re open(my ($fh), "<", $filename) or return; binmode $fh; read $fh, my ($buffer), 2048; $buffer =~ s/[[:^print:]]/=/g; my %info = map +($_ => /$_=([^=]+)/i), qw(artist title album comment genre tracknumber); $info{track} = delete $info{tracknumber} if exists $info{tracknumber}; return %info; } 1;
It is probably still better to implement something that conforms to the docs (http://reactor-core.org/ogg-tag-standard.html, http://www.xiph.org/ogg/vorbis/doc/v-comment.html).

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: OGG Vorbis tag parsing in pure perl
by skx (Parson) on Dec 17, 2002 at 23:26 UTC

     Thanks for the feedback, I like the trick with the map, I knew the tag finding could be done in a better manner - I don't know why I didn't see that earlier!

     I should really be using one of the real tag parsing packages; this code is meant to be a fallback position if OGG::Vorbis can't be found.

     (I prefer to have code running minimally rather than dying when modules aren't found; if at all possible)

    Steve
    ---
    steve.org.uk
Re: Re: OGG Vorbis tag parsing in pure perl
by Wonko the sane (Curate) on Dec 31, 2002 at 15:50 UTC
    Aristotle,

    I don't understand the map syntax used in your code example.
    I know you marked this as untested, but thought I would ask for clarification
    in case this was not a typo of some sort and I was missing something.
    Does the "map +( )" syntax have a special purpose? I was unable to get the code to work as is,
    and I had to modify my version a bit to get it to work.

    my %info = map { $_ => $buffer =~ /$_=([^=]+)/i } qw(artist title albu +m genre comment);

    Any help is much appreciated, as always.
    Wonko.
      No real difference. The + is just there as a hint for Perl that what's in the parens should be map's expression, not a parenthesized list of arguments much like you might say print +($x + 1) * 3; to get the meaning of print(($x + 1) * 3); rather than the default parsing result of (print($x + 1)) * 3;. I've had success making it work that way for map, but apparently the rules are not quite so simple as I thought. Functionally, it's supposed to be just the equivalent to what you used. It just bugged me to have to use a BLOCK whenever I'm mapping an array to a hash.

      Makeshifts last the longest.