#!/usr/bin/perl use strict; use warnings; use Net::Ping::External qw(ping); # add more modules as needed... my $testfolder = "/Users/Appy/Desktop"; # other variables should be declared in the "myping" sub, or wherever they're needed open( my $infh, "<", "$testfolder/testfile.txt" ) or die "$testfolder/testfile.txt: $!"; my @file = <$infh>; chomp @file; foreach (@file) { my ( $method, $count, $uri ) = split; # I'm GUESSING that each line has 3 fields next unless ( $method eq 'ping' and # I'm GUESSING that "ping" is line-initial $count =~ /^\d+$/ and $count > 0 ); myping( $uri, $count); } sub myping { my ( $newURI, $count ) = @_; my $tries = 0; my $alive; while ( ++$tries < $count ) { $alive = ping(host => "$newURI"); last if ( $alive ); # you might want to put a sleep call here } if ( ! $alive ) { # do whatever needs to be done... } }