Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl function call confused me

A perl function call confused me, could anyone help me?

catFiles called like this:

catFiles( \@LINKFILES => "$output_prefix.links" );

catFiles function define:

sub catFiles {

    unlink("$_[1]") if(exists $_[1]);
    system qq( cat "$_" >> "$_[1]" ) for @{$_[0]};
}

I don't know why there are => there which I think it should be a ,.

like image 853
user1744416 Avatar asked Apr 07 '13 02:04

user1744416


1 Answers

=> is (almost) equivalent to , in Perl. (See the "official" documentation for the differences.)

Usually it's used when defining a hash to indicate the relationship between key and value:

my %hash = (
  'a' => 1,
  'b' => 2,
);

We could write it as my %hash = ('a', 1, 'b', 2); with no change in behavior but that doesn't look as nice. You could even do my $hash = ('a', 1 => 'b', 2); but that's just confusing.

Similarly, in your case you could write the code as

catFiles(\@LINKFILES, "$output_prefix.links");

and it would do the same thing as the => version.

Here it's getting used as syntactic sugar, I guess to indicate that the contents of @LINKFILES will get concatenated into "$output_prefix.links".

like image 136
miorel Avatar answered Oct 20 '22 18:10

miorel