Вопрос

I have never done perl programming but I am looking at following Perl code and it confused me:

sub read_pds
{
   my $bin_s;
   my $input_pds_file = $_[0];
  open(my $fh, '<', $input_pds_file) or die "cannot open file $input_pds_file";
  {
    local $/;
    $bin_s = <$fh>;
  }
  close($fh);
  return $bin_s;
}

I am looking at the code above and though that it will not return any value since there is no return type defined there.

But at the bottom it is returning a value. Now How would I know what is the type of the value since it does not show any value when I add watch on it using Komodo..

Any ideas?

Это было полезно?

Решение 2

Get first argument passed to function call:

my $input_pds_file = $_[0];

Open file to read:

open(my $fh, '<', $input_pds_file) or die "cannot open file $input_pds_file";

Sets input record separator to none (default is new line sequence: CR or LF or CRLF):

local $/;

And read whole file to variable:

$bin_s = <$fh>;

Why read whole file at once? Because "diamond operator": <> read data from handle until find input record separator (which is cleared above).

And finally, returns one big string:

return $bin_s;

Другие советы

Perl doesn't have types as you're thinking of them, but the returned value will be a string (the contents of $input_pds_file. The <$fh> reads a single line/record from $fh, and because $/ (the input record separator) is set to undef, that will be the whole file.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top