#!/usr/bin/perl package main; use strict; # get an instance of the example code my $obj = new FoundationExamples(); # execise basic string operations $obj->basicStrings(); # intentional bad plist load my $plist = $obj->basicPlist("/bad/path.plist"); # this plist should be ok $plist = $obj->basicPlist("/Library/Preferences/SystemConfiguration/preferences.plist"); # print part of the plist $obj->printValueForKey($plist, "CurrentSet"); # try accessing the plist via objectAtIndex_() # ok this fails, just comment it out for now #$obj->perlValue($plist->objectAtIndex_(1)); # check class of an object my $object = $plist->objectForKey_("System"); if ( $object && $$object ) { $obj->getClass($object); } # get an object from the plist my @keyList = ("System", "System", "ComputerName" ); if ( $plist and $$plist) { my $computerName = $obj->getPlistObject( $plist, \@keyList); if ( $computerName and $$computerName ) { print "ComputerName: " . $obj->perlValue( $computerName ) . "\n"; } else { die "Could not find the value.\n"; } } else { die "Error loading file.\n"; } ##################################### package FoundationExamples; use Foundation; use strict; sub new { my $class = shift; $class = ref $class || $class; my $self = {}; bless $self, $class; return $self; } sub basicStrings { my $self = shift; # These are the lines we care about:These my $s1 = NSString->stringWithCString_("Hello "); my $s2 = NSString->alloc()->initWithCString_("World"); my $s3 = $s1->stringByAppendingString_($s2); printf "%s\n", $s3->UTF8String(); $s2->release(); } sub basicPlist { my $self = shift; my $file = shift; my $plist = NSDictionary->dictionaryWithContentsOfFile_( $file ); if ( ! $plist or ! $$plist ) { print "One of the following failed " . '$plist: ' . $plist . ' $$plist: ' . $$plist . "\n"; } else { print "Successfully read the plist.\n"; } return $plist; } sub printValueForKey { my $self = shift; my $plist = shift; my $key = shift; my $value = $plist->objectForKey_($key); print $self->perlValue($value) . "\n"; } sub perlValue { my $self = shift; my $object = shift; return $object->description()->UTF8String(); } sub getClass { my $self = shift; my $object = shift; if ( $object->isKindOfClass_( NSArray->class ) ) { print "$object is a array\n"; } elsif ( $object->isKindOfClass_( NSDictionary->class ) ) { print "$object is a dictionary\n"; } else { print "$object is something else, print its value:\n"; print $self->perlValue( $object ) . "\n"; } } sub getPlistObject { my $self = shift; my $object = shift; my $keysIndexes = shift; foreach my $keyIndex ( @$keysIndexes ) { if ( $object and $$object ) { if ( $object->isKindOfClass_( NSArray->class ) ) { $object = $object->objectAtIndex_( $keyIndex ); } elsif ( $object->isKindOfClass_( NSDictionary->class ) ) { $object = $object->objectForKey_( $keyIndex ); } else { print STDERR "Unknown type (not an array or a dictionary):\n"; return; } } else { print STDERR "Got nil or other error for $keyIndex.\n"; return; } } return $object; }