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

Dear perl monks I'm currently having a brain fart for some reason I can't write a simple subroutine that concatenate the information inside of it something like
$A= concat( 'ab', 'cd',...) print $A; sub concat{?????} The program output should be: abcd.....

Replies are listed 'Best First'.
Re: Concatenation(Brain Fart)
by davido (Cardinal) on Aug 18, 2011 at 03:44 UTC

    join can take arbitrary lists, whereas . (dot) concatenates the left hand side with the right hand side.

    sub concat { return join '', @_; }

    Or if you only want to work with two args, there is the dot operator:

    sub concat { return $_[0] . $_[1];

    Interpolation also works:

    sub concat { local $" = ''; return "@_"; }

    Unless you can think of a good reason otherwise, the first solution is probably the most clear and flexible -- a more generalized solution.

    I think Perl was given the smallest possible operator (dot) for concatenation of strings to make the C people feel bad.


    Dave

Re: Concatenation(Brain Fart)
by jwkrahn (Abbot) on Aug 18, 2011 at 03:46 UTC

    It looks like you want join

    $A = join '', 'ab', 'cd',...;
      Thank you that worked perfectly