Create a Reddit::Client instance

Method 1: Create an instance and log in at the same time:

use Reddit::Client;

my $reddit = new Reddit::Client(
    user_agent      => "Test Reddit::Client script 1.0 by /u/myusername",
    client_id       => "DFhtrhBgfhhRTd",
    secret          => "KrDNsbeffdbILOdgbgSvSBsbfFs",
    username        => "reddit_username",
    password        => "reddit_password"               );

Method 2: Create the instance and log in later. Useful for scripts that switch between several users.

my $reddit = new Reddit::Client( user_agent => "test Reddit::Client 1.0 by /u/myusername" );

$reddit->get_token(
    client_id       => "DFhtrhBgfhhRTd",
    secret          => "KrDNsbeffdbILOdgbgSvSBsbfFs",
    username        => "reddit_username",
    password        => "reddit_password"               );

Check your inbox

my $me = $reddit->me();

print "You've got mail!" if $me->{has_mail};
$reddit->submit_link(
    subreddit       => 'test',
    title           => 'Perl is still alive!',
    url             => 'http://www.perl.org'           );

Submit a text post

$reddit->submit_text(
    subreddit       => 'test',
    title           => 'Reddit::Client test thread',
    text            => 'What year is this??'           );

Get posts from a subreddit or multi

my $posts = $reddit->get_links(subreddit=>'test', limit=>5);

foreach my $post (@$posts) {

    print $post->{title};

    if ($post->{is_self}) {
            print $post->{selftext};
    } else {
            print $post->{url};
    }
}

Get comments from a subreddit or multi

my $cmts = $reddit->get_subreddit_comments(subreddit=>'test', view=>'controversial');

foreach my $cmt (@$cmts) {
    print "$cmt->{author} says: $cmt->{body}\n";
}

Get a post using its fullname

my $post = $reddit->get_link('t3_3o6sbn');

my $fullname = $post->reply("What year is this??");

# Changed my mind...

$reddit->delete($fullname);

Get a comment using its fullname

my $cmt = $reddit->get_comment('t1_cvujo4u');

my $permalink = $cmt->get_permalink();

Show newly created subs

my $subs = $reddit->new_subreddits();

foreach my $sub (@$subs) {
    print "$sub->{display_name} is NSFW" if $sub->{over18};
}

Send a private message

$reddit->send_message(
    to      => 'someuser',
    subject => 'Private message subjects are 100 characters max',
    text    => 'What year is this??'
);

Catch exceptions

Any application that's making requests over the internet should be able to handle it if the request fails. Reddit in particular is prone to 503 errors when under heavy load.

One way to do this is with Try::Tiny. It provides a minimal try/catch syntax. Here is an example script that loops in the background forever, handling bad requests when they happen:

use Try::Tiny;

while(1) {

    my $cmts;

    try {
            $cmts = $reddit->get_subreddit_comments(subreddit=>'all', limit=>0);
    } catch {
            print "Something went wrong: $_";
    };

    sleep 15;
}

Note the semicolon after the catch block. It's a common source of errors when using Try::Tiny. Whether you use a try block by itself, a try/catch, or a try/catch/finally, it needs to end with semicolon after the last block.

A slightly more useful example:

while(1) {

    my $cmts;

    next if ! try {
            $cmts = $reddit->get_subreddit_comments(subreddit=>'all', limit=>0);
    };

} continue { sleep 15; }

This makes a couple important changes:

  1. It skips the rest of the block if there's an error, and
  2. It moves the sleep statement to a continue block. Otherwise it would get skipped along with everything else, and the next loop would begin immediately with no delay. Probably not what you want.

When a try block fails it returns undef, so we can simply check to see if it returns true or not. However, when a catch block fails, it returns the value of the last executed statement, just like a function will. So to add a catch block to the above, we do this:

while(1) {

    my $cmts;

    next if ! try {
            $cmts = $reddit->get_subreddit_comments(subreddit=>'all', limit=>0);
    } catch {
            print "Something went wrong: $_";
            0;
    };

} continue { sleep 15; }

The 0 ensures that it returns false regardless.