mattack (paintitmatt) wrote in perl,
  • Music: King Tubby & Lee Perry - Splash Out [Dub]

question about arrays and references

I wrote a script that will post to twitter. It takes a command line argument or reads a random line from a text file, and posts the result. I've got it working, but I think code could be a little cleaner.
Basically, I want to know if there is a way to put each element in an array ( like @ARGV) into a string.
For instance, if I run perl post-to-twitter.pl here is a tweet, I'd like to turn "here is a tweet" into a string so I can put it in a scalar like $post.
I think I could use a reference, but I have a hard time wrapping my head around the concept. I really want to turn the last 4 or 5 lines into one.

Thanks...



#!/usr/bin/perl -w
use strict;
use Net::Twitter;

my $post;

# determine what to post
if (@ARGV) {
    $post = \@ARGV;
    print"@$post\n"; # only in for debugging
} else { # open file of posts and pick a line at random
    open (TALK, 'file.txt') or die "can't open file: $!\n";
    srand;
    rand($.) < 1 && ($post = $_) while <TALK>;
    close TALK;
    print "$post"; # only in for debugging
}

# post to twitter
my $twit = Net::Twitter ->new(
    username => "username",
    password => "password",
      source => "web"
);
if (@ARGV) {
    my $result = $twit->update("@$post\n");
} else {
    my $result = $twit->update("$post");
}
</talk>


Oh yeah, if the command line argument has an apostrophe, the script breaks... Why is that?
  • Post a new comment

    Error

    default userpic
  • 10 comments