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

I was reading an interesting article in The Perl Journal right here that included some code for an RPC server daemon and client. While I recommend the article on its own merits, I was a little mystified by the following assignment in a subroutine:

my ($keywords) = "@_";

Why would Jon Orwant (of Camel fame and publisher of TPJ) wrap his subroutine arguments array in double quotes like that? I don't argue that it will work, but why add the overhead of interpolation? Anyone got some mystical insights on this?

Thanks in advance... Guv

Replies are listed 'Best First'.
Re: Seeking enlightenment: is there a reason for this unusual syntax?
by dvergin (Monsignor) on Feb 17, 2002 at 03:55 UTC
    Consider this:
    my @ary = ('a','b','c','d'); print @ary; # prints "abcd" as four 1-char string values print "\n"; print "@ary"; # prints "a b c d" as a single string value
    An array in double-quotes interpolates with the array values separated by the value of the special variable: $" (which is space by default).

    So the line you cite creates a single string consisting of all the concatenated elements of @_ separated by spaces.

    HTH, David
     

Re (tilly) 1: Seeking enlightenment: is there a reason for this unusual syntax?
by tilly (Archbishop) on Feb 17, 2002 at 04:01 UTC
    Just some syntactic sugar, that is all.

    Writing "@foo" is just another way to write join($", @foo). And $" defaults to a space. You probably understand the construct if you can figure out this snippet:

    $" = "'\nThen you have: '"; print "The first letter is '@{[A..Z]}'\n";
Re: Seeking enlightenment: is there a reason for this unusual syntax?
by Anonymous Monk on Feb 17, 2002 at 03:52 UTC
    my ($keywords) =  join$",@_;
Re: Seeking enlightenment: is there a reason for this unusual syntax?
by theguvnor (Chaplain) on Feb 17, 2002 at 04:16 UTC

    D'oh!

    Yeah, I see the light now. That was all it took - a little smack on the head. Thanks for the quick responses monks!

    ..Guv