Mayank Lahiri, a researcher at the University of Illinois at Chicago, has thrown down a golfing challenge for his 9-line program to send the FourSquare geolocation API randomly skewed arbitrary data.

I've already made him aware of my 5-line version which uses only the simplest of changes, and I am sure there is more to strip out of this. What would Perl Golf be without involving the Monks?

Unlike some golf challenges, this challenge includes any "standard" (which I take to mean "part of the core distribution as shipped") Perl modules. He's also concerned more with lines of code (let's assume logical lines of code and not just really long concatentations of different logical steps) than with character count, at least from his description. The full rules are included on the linked page. He promises to include any variations which use fewer lines and meet the specs he put forth on his journal. Code clarity is not one of his specs.

The original point of his journal entry is the insecurity through obscurity of the API in question. The golf challenge I think is just for a bit of fun. Once a program is under ten lines, I don't think it being any shorter is of much importance to prove his point. Still, as a chance to show off Perl's power and expressiveness, this is a golden opportunity. Bragging rights for the golfer with the smallest solution don't hurt, either.

  • Comment on Nine-line Perl example of gaming a geolocation service and an open invitation to golf it

Replies are listed 'Best First'.
Re: Nine-line Perl example of gaming a geolocation service and an open invitation to golf it
by ikegami (Patriarch) on Aug 21, 2010 at 18:10 UTC
    Honestly, his program consists mostly of a long URL. What does he hope to get out of this?

    Update: Screw golf, it's not even good code.

    • Useless srand.
    • What's with the \r\n at the end of the body?
    • Specifying both Proto and Type is redundant.
    • join would make the code easier to read. Bah, even breaking the long url more logically would be more readable.

    The following is so much better, and by his count, it's only 4 lines even though it's practically not golfed:

    #!/usr/bin/perl -MIO::Socket my $body = join('&', "vid=$ARGV[0]", "private=0", "geolat=".( $ARGV[1] + rand() * 0.0001 - 0.00005 ), "geolong=".( $ARGV[2] + rand() * 0.0001 - 0.00005 ), ); sleep(rand()*600); print( { my $sock = IO::Socket::INET->new( PeerAddr => 'api.foursquare.com:80', Proto => 'tcp', ) or die $! } join("\r\n", 'POST /v1/checkin HTTP/1.1', 'Host: api.foursquare.com', 'User-Agent: Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en)' .' AppleWebKit/420+ (KHTML, like Gecko)' .' Version/3.0 Mobile/1C10 Safari/419.3', 'Content-Type: application/x-www-form-urlencoded', 'Authorization: Basic XXXXXX', 'Content-length: '.length($body), "", $body, ), ); $_ = <$sock>;

    I'm not convinced that long User-Agent is required. And you can surely get away with not waiting for the reply. (Could fail, but not likely.)

    I was tempted to write the User-Agent as equivalent

    'User-Agent: Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en)', 'User-Agent: AppleWebKit/420+ (KHTML, like Gecko)', 'User-Agent: Version/3.0 Mobile/1C10 Safari/419.3',
Re: Nine-line Perl example of gaming a geolocation service and an open invitation to golf it
by james2vegas (Chaplain) on Aug 21, 2010 at 18:56 UTC
    I went a different route, and not being in possession of a foursquare account, this is untested. I went with LWP instead:
    Update: I guess since LWP is 'non-standard', this doesn't qualify</shrug>
    #!/usr/bin/perl use LWP::UserAgent; sleep(rand()*600); my ($lt, $lg) = map { $ARGV[$_]+rand()*0.0001-0.00005 } 1..2; my $lwp = LWP::UserAgent->new(agent=>'Mozilla/5.0 (iPhone; U; CPU like + Mac OS X; en) '. 'AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1C10 Safari/4 +19.3'); $lwp->default_header(Authorization => 'Basic XXXXXX'); $lwp->post('http://api.foursqure.com/v1/checkin', {vid=>$ARGV[0],priva +te=>0, geolat=>$lt,geolong=>$lg});

    And the slightly better looking version: