in reply to Subroutines....return directory file is in and return contents in array

sub contents { open (FH, "< $FileInfoName") or die "Can't open $FileInfoName for re +ad: $!"; my @lines = <FH>; # return @lines; # 1st problem: in scalar context, number of lines is + returned # 2nd problem: next line is never reached close FH or die "Cannot close $FileInfoName: $!"; return join("", @lines); # fix: one big string with all lines concate +nated # See also File::Slurp }

See module File::Basename to extract the directory name (dirname()) from the path, you already have. See module Cwd to get current working directory - if required.

Update: oops - you wanted contents() to return an array (Perl: list) -- then you can keep the return @lines; (in last line) and change the callers context as toolic suggested.

Maybe two other suggestions?

  • Avoid globals like $FileInfoName, use a parameter to contents() instead.
  • A more contemporary way is the three-arg-open plus avoiding globs like FH:
    e.g: open(my $file, '<', $FileInfoName) or die "..."; my @lines = <$file>; ...
    (and maybe chose a better name than $file (as I did here ;-) - something that is meaningful in the given context.