use constant SEARCH_TERM => "some_search_term"; parse_from_twitter_stream(); sub parse_from_twitter_stream { my $user = 'XXXXX'; my $password = 'YYYYY'; my $stream = Net::Twitter::Stream->new ( user => $user, pass => $password, callback => \&got_tweet, connection_closed_cb => \&connection_closed, track => SEARCH_TERM); sub connection_closed { sleep 1; warn "Connection to Twitter closed"; parse_from_twitter_stream(); } sub got_tweet { my ( $tweet, $json ) = @_; # a hash containing the tweet # and the original json warn "Got tweet"; #I have more code here but it's snipped } }