Extract slide titles from a PowerPoint document using Perl's Win32::OLE

I finally figured this out in response to a question on Stackoverflow.com.

The trick was to look at the type of placeholder to identify titles on slides.

#!/usr/bin/perl

use strict; use warnings;
use Try::Tiny;
use Win32::OLE;
use Win32::OLE::Const qw( Microsoft.PowerPoint );
use Win32::OLE::Enum;

$Win32::OLE::Warn = 3;

my $ppt = get_ppt();

my $presentation = $ppt->Presentations->Open('test.ppt', 1);
my $slides = Win32::OLE::Enum->new( $presentation->Slides );

SLIDE:
while ( my $slide = $slides->Next ) {
    printf "%s:\t", $slide->Name;
    my $shapes = Win32::OLE::Enum->new( $slide->Shapes );
    SHAPE:
    while ( my $shape = $shapes->Next ) {
        my $type = $shape->PlaceholderFormat->Type;
        if ( $type == ppPlaceholderTitle
                or $type == ppPlaceholderCenterTitle
                or $type == ppPlaceholderVerticalTitle
        ) {
            print $shape->TextFrame->TextRange->text;
            last SHAPE;
        }
    }
    print "\n";
}

$presentation->Close;

sub get_ppt {
    my $ppt;

    try {
        $ppt = Win32::OLE->GetActiveObject('PowerPoint.Application');
    }
    catch {
        die $_;
    };

    unless ( $ppt ) {
        $ppt = Win32::OLE->new(
            'PowerPoint.Application', sub { $_[0]->Quit }
        ) or die sprintf(
            'Cannot start PowerPoint: %s', Win32::OLE->LastError
        );
    }

    return $ppt;
}