KEMBAR78
Hacking Mac OSX Cocoa API from Perl | PDF
•   #import <Foundation/Foundation.h>

    int main(int argc, char** argv) {
        NSAutoreleasePool* pool =
             [[NSAutoreleasePool alloc] init];

        // Objective-C code here
        NSLog(@”Hello World!”);

        [pool drain];
        return 0;
    }
•   #include   "EXTERN.h"
    #include   "perl.h"
    #include   "XSUB.h"
    #include   "ppport.h"

    XS(func) {
        dXSARGS;

        // code here

        XSRETURN(0);
    }

    XS(boot_Foo) {
        newXS("Foo::xs_function", func, __FILE__);
    }
• package Foo;
  use strict;
  use XSLoader;

  XSLoader::load __PACKAGE__, $VERSION;

  1;
• use   Foo;

  Foo::xs_function();
•   use inc::Module::Install;

    # some basic descriptions here

    use_ppport '3.19';

    WriteAll;
•   #include   "EXTERN.h"
    #include   "perl.h"
    #include   "XSUB.h"
    #include   "ppport.h"

    // undefine Move macro, this is conflict to Mac OS X QuickDraw API.
    #undef Move

    #import <Foundation/Foundation.h>

    XS(hello) {
        dXSARGS;

        NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
        NSLog(@"Hello!");
        [pool drain];

        XSRETURN(0);
    }

    XS(boot_Hello) {
        newXS("Hello::hello", hello, __FILE__);
    }
• #include "EXTERN.h"
  #include "perl.h"
  #include "XSUB.h"
  #include "ppport.h"
• #undef   Move
• XS(function)   {
      dXSARGS;
      // code here
      XSRETURN(0);
  }
• XS(function)   {
      dXSARGS;
      // code here
      ST(0) = some_sv;
      XSRETURN(1);
  }
• XS(function)   {
      dXSARGS;
      // code here
      ST(0) = some_sv;
      ST(1) = some_sv2;
      XSRETURN(2);
  }
•   XS(function) {
        dXSARGS;

        SV* sv_args1 = ST(0);
        SV* sv_args2 = ST(1);

        // code here

        ST(0) = some_sv;
        ST(1) = some_sv2;
        XSRETURN(2);
    }
•   XS(function) {
        dXSARGS;

        if (items < 2) {
            Perl_croak(aTHX_ "Usage: function($args1, $args2)");
        }

        SV* sv_args1 = ST(0);
        SV* sv_args2 = ST(1);

        // code here

        ST(0) = some_sv;
        ST(1) = some_sv2;
        XSRETURN(2);
    }
STRLEN len;
char* c = SvPV(sv, len);
NSString* str = [NSString stringWithUTF8String:c];




SV* sv = sv_2mortal(newSV(0));
sv_setpv(sv, [str UTF8String]);
NSNumber* n;
if (SvNOKp(sv)) {
    n = [NSNumber numberWithDouble:(double)SvNVX(sv))];
}
else if (SvIOK_UV(sv)) {
    n = [NSNumber numberWithDouble:(double)SvUV(sv))];
}
else if (SvIOKp(sv)) {
    n = [NSNumber numberWithDouble:(double)SvIV(sv))];
}



SV* sv = sv_2mortal(newSVnv([n doubleValue]));
• use   Cocoa::Growl ':all';

  my $installed = growl_installed();
  my $running   = growl_running();
• use   Cocoa::Growl ':all';

  # register application
  growl_register(
      app            => 'My growl script',
      icon           => '/path/to/icon.png',
                   # or 'http://urlto/icon'
      notifications =>
          [qw(Notification1 Notification2)],
  );
• use   Cocoa::Growl ':all';

  # show growl notification
  growl_notify(
      name        => 'Notification1',
      title       => 'Hello!',
      description => 'Growl world!',
  );
•   use Cocoa::EventLoop;
    use Cocoa::Growl ':all';

    growl_register(
        name          => 'test script',
        notifications => ['test notification'],
    );

    my $wait = 1;
    growl_notify(
        name         => 'test notification',
        title        => 'Hello',
        description => 'Growl World!',
        on_click => sub {
             warn 'click';
             $wait = 0;
        },
        on_timeout => sub {
             warn 'timeout';
             $want = 0;
        },
    );

    Cocoa::EventLoop->run_while(0.1) while unless $wait;
use AnyEvent;
use Cocoa::EventLoop;

# then all anyevent based api use
Cocoa::EventLoop!
•   use AnyEvent;
    use Cocoa::EventLoop;
    use Cocoa::Growl;

    my $cv = AE::cv;
    growl_notify(
        name         => 'test notification',
        title        => 'Hello',
        description => 'Growl World!',
        on_click => sub {
             warn ‘click’;
             $cv->send;
        },
        on_timeout => sub {
             warn ‘timeout’;
             $cv->send;
        },
    );
    $cv->recv;
Hacking Mac OSX Cocoa API from Perl

Hacking Mac OSX Cocoa API from Perl

  • 12.
    #import <Foundation/Foundation.h> int main(int argc, char** argv) { NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; // Objective-C code here NSLog(@”Hello World!”); [pool drain]; return 0; }
  • 17.
    #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "ppport.h" XS(func) { dXSARGS; // code here XSRETURN(0); } XS(boot_Foo) { newXS("Foo::xs_function", func, __FILE__); }
  • 18.
    • package Foo; use strict; use XSLoader; XSLoader::load __PACKAGE__, $VERSION; 1;
  • 19.
    • use Foo; Foo::xs_function();
  • 20.
    use inc::Module::Install; # some basic descriptions here use_ppport '3.19'; WriteAll;
  • 26.
    #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "ppport.h" // undefine Move macro, this is conflict to Mac OS X QuickDraw API. #undef Move #import <Foundation/Foundation.h> XS(hello) { dXSARGS; NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; NSLog(@"Hello!"); [pool drain]; XSRETURN(0); } XS(boot_Hello) { newXS("Hello::hello", hello, __FILE__); }
  • 27.
    • #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "ppport.h"
  • 28.
  • 29.
    • XS(function) { dXSARGS; // code here XSRETURN(0); }
  • 30.
    • XS(function) { dXSARGS; // code here ST(0) = some_sv; XSRETURN(1); }
  • 31.
    • XS(function) { dXSARGS; // code here ST(0) = some_sv; ST(1) = some_sv2; XSRETURN(2); }
  • 32.
    XS(function) { dXSARGS; SV* sv_args1 = ST(0); SV* sv_args2 = ST(1); // code here ST(0) = some_sv; ST(1) = some_sv2; XSRETURN(2); }
  • 33.
    XS(function) { dXSARGS; if (items < 2) { Perl_croak(aTHX_ "Usage: function($args1, $args2)"); } SV* sv_args1 = ST(0); SV* sv_args2 = ST(1); // code here ST(0) = some_sv; ST(1) = some_sv2; XSRETURN(2); }
  • 34.
    STRLEN len; char* c= SvPV(sv, len); NSString* str = [NSString stringWithUTF8String:c]; SV* sv = sv_2mortal(newSV(0)); sv_setpv(sv, [str UTF8String]);
  • 35.
    NSNumber* n; if (SvNOKp(sv)){ n = [NSNumber numberWithDouble:(double)SvNVX(sv))]; } else if (SvIOK_UV(sv)) { n = [NSNumber numberWithDouble:(double)SvUV(sv))]; } else if (SvIOKp(sv)) { n = [NSNumber numberWithDouble:(double)SvIV(sv))]; } SV* sv = sv_2mortal(newSVnv([n doubleValue]));
  • 43.
    • use Cocoa::Growl ':all'; my $installed = growl_installed(); my $running = growl_running();
  • 44.
    • use Cocoa::Growl ':all'; # register application growl_register( app => 'My growl script', icon => '/path/to/icon.png', # or 'http://urlto/icon' notifications => [qw(Notification1 Notification2)], );
  • 45.
    • use Cocoa::Growl ':all'; # show growl notification growl_notify( name => 'Notification1', title => 'Hello!', description => 'Growl world!', );
  • 46.
    use Cocoa::EventLoop; use Cocoa::Growl ':all'; growl_register( name => 'test script', notifications => ['test notification'], ); my $wait = 1; growl_notify( name => 'test notification', title => 'Hello', description => 'Growl World!', on_click => sub { warn 'click'; $wait = 0; }, on_timeout => sub { warn 'timeout'; $want = 0; }, ); Cocoa::EventLoop->run_while(0.1) while unless $wait;
  • 48.
    use AnyEvent; use Cocoa::EventLoop; #then all anyevent based api use Cocoa::EventLoop!
  • 49.
    use AnyEvent; use Cocoa::EventLoop; use Cocoa::Growl; my $cv = AE::cv; growl_notify( name => 'test notification', title => 'Hello', description => 'Growl World!', on_click => sub { warn ‘click’; $cv->send; }, on_timeout => sub { warn ‘timeout’; $cv->send; }, ); $cv->recv;