Objective-C decisions vs Perl descisions

I’m comparing Objective-C decision making with our beloved Perl decision making. Objective-C supports the if, else, else if & switch. Lets write some Objective-C code and write the Perl in parallel.

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

    @autoreleasepool {

        int tester = 1;
        if (tester == 1){
            printf("The tester value was %i\n", tester);
        }

    }
    return 0;
}

Now here’s our Perl version:

#! /usr/bin/perl -w

$tester = 1;
if ($tester == 1){
 printf("The tester value was %i\n", $tester);
}

I needed to add the $ sigil and remove the int declaration, but aside from that, nothing…
Next we’ll add some else code to compare the two again:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

    @autoreleasepool {

        int tester = 2;
        if (tester == 1){
            printf("The tester value was detected as 1\n");
        }else if(tester == 2){
            printf("The tester value was detected as 2\n");
        }else{
            printf("The tester value was %i\n", tester);
        }

    }
    return 0;
}

And now into Perl:

#! /usr/bin/perl -w

$tester = 2;

if ($tester == 1){
        printf("The tester value was detected as 1\n");
}elsif($tester == 2){
        printf("The tester value was detected as 2\n");
}else{
        printf("The tester value was %i\n", $tester);
}

Small changes, note that Perl uses elsif not else if. Apparently there is some perfectly valid reason for the spelling of the Perl version!
The final Objective-C decision maker is the switch statement:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

    @autoreleasepool {

        int tester = 2;

        switch(tester){
            case 1:
                printf("The tester value was detected as 1\n");
                break;
            case 2:
                printf("The tester value was detected as 2\n");
                break;
            default:
                printf("The tester value was %i\n", tester);
                break;
        }

    }
    return 0;
}

And now the increasingly varied Perl:

#! /usr/bin/perl -w

use 5.10.0;

$tester = 2;

given ($tester){
        when (1){
                printf("The tester value was detected as 1\n");
        }
        when (2){
                printf("The tester value was detected as 2\n");
        }
        default {
                printf("The tester value was %i\n", $tester);
        }
}

Perl never really had a ‘switch/case so a module was added, which I believe is being depreciated. The ‘given/when‘ has been borrowed from the illusive Perl 6 as of Perl 5.10, but in order to access the 5.10 features you have to use the use 5.10.0 line.

I tend to use this sort of test in the boolean form quite a lot in Perl. I only really want to know whether the test variable has a value or not, such as a return value from a function. Here is a bit of code in Objective-C which kind of illustrates what i mean:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

    @autoreleasepool {

        int tester;

        if(! tester) printf("tester has a value of %i\n", tester);
    }
    return 0;
}

The output is: tester has a value of 0
I’ve done this slightly differently to show the different syntax. As there is only one line following the if, we don’t need to use curly braces, and we an even just run it out on the same line, just to make it more confusing! Here is the Perl equivalent:

#! /usr/bin/perl -w
$tester;
if (! $tester){printf("tester has a value of %i\n", $tester)}

Same output as before.

This entry was posted in C, Perl and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *