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

Hello Monks I, a newby at PERL, have been given the task of debugging some PERL code. Although I've been told that this is improbable, the nay-sayers have probably not heard of the Monks. Therefore, I submit to you the following:
sub GetInfDataFiles { my ($path) = @_; my ($currpath, $age, $filea, $fileb, @allfiles, @list); $currpath = cwd(); chdir $path; $age = -7; @allfiles = `find .. -name "r*" -type f -mtime $age`; chomp( @allfiles ); foreach (@allfiles) { chomp(); s/\.\///; } @list = sort { (-M "$path/$b") <=> (-M "$path/$a") } @allfiles; chdir $currpath; print ( "@list" ); return @list; } # GetInfDataFiles()
The error returned states "Use of uninitialized value" on the "@list = sort" line. Can you make some suggestions? I know that this object is dealing with passed variables ($path), and this seems to be OK; perhaps you could inform as to the expectations that PERL has? Thx PH

Replies are listed 'Best First'.
Re: Array error!
by Pug (Monk) on May 01, 2001 at 23:07 UTC
    I ran it a few times it looks like s/\.\/// is your problem.

    What is happening is that your files in @allfiles looks something like this.
    ../alya/problem1
    ../yltra/result
    the s/\.\/// removes the second dot and the / so your array looks like this.
    .alya/problem1
    .yltra/result
    So try removing s/\.\///;

(boo)Re: Array error!
by boo_radley (Parson) on May 01, 2001 at 22:49 UTC
    Giving your code the once-over, I can imagine that GetInfDataFiles is being called with no parameter, or a parameter that is undef. May I suggest inserting
    unless ($path) {die "empty or no parameter passed to GetInfDataFiles!" +}

    As you say you're a total newbie, I'll point out that unless is one of perl's cooler control structures; it's the complement to if -- it tests for falseness, where if tests for truth. For this type of test using if, you'd need to write
    if (! $path) {die "empty or no parameter passed to GetInfDataFiles!"}

    and I think the negation inside the if isn't too much clearer than the unless.
    OK, so now that you know the unless will only execute when its fails, whenever your sub dies like this, you'll know that the $code was blank.
      Ack! Please don't do this. I have to work with this kind of syntax on a daily basis and it really isn't nice, considering there are so much more cleaner ways of doing this. This is Perl, after all ;)
      my $path = shift or die "No path passed to GetInfDataFiles\n"
      or even:
      my $path = shift; die "No path passed to GetInfDataFiles\n" unless $path;


      ar0n ]