Seventh has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I have a script that reads in lines from an input file called ccrConfig.cfg. If the file exists, it works perfectly, but if the file is missing, it terminates with an error. "Can't return outside a subroutine at ./ccrutil.pl line 59." (line 59 is the 'return unless' statement)

I'm quite the Perl newbie, so I'm not sure where exactly to put the 'die' statement in the following loop:
if ($read) { my $cvsfilecount = 0; print ("\nCCRUtil: Reading configuration\n\n"); my $fh2 = new IO::File("<ccrConfig.cfg"); return unless($fh2); my @state2 = <$fh2>; foreach my $f (@state2) { chomp($f); dprint( "Checking out project file: $f\n"); `cvs co $f`; $cvsfilecount++ } print "\nCCRUtil complete: $cvsfilecount files added from CVS.\n"; }


Any insight is as always very much appreciated, and thanks again for all the help so far.

Replies are listed 'Best First'.
Re: Question regarding input file / script termination
by holli (Abbot) on Feb 02, 2005 at 17:05 UTC
    Well, you just canīt use return if you are not inside a subroutine. Change your code to
    if ($read) { my $cvsfilecount = 0; print ("\nCCRUtil: Reading configuration\n\n"); my $fh2 = new IO::File("<ccrConfig.cfg"); if ($fh2) { my @state2 = <$fh2>; foreach my $f (@state2) { chomp($f); dprint( "Checking out project file: $f\n"); `cvs co $f`; $cvsfilecount++ print "\nCCRUtil complete: $cvsfilecount files added from CVS.\n"; } else { warn "could not create filehandle: $!\n"; } }

    holli, regexed monk