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

Here is one strange behavior that i am facing from perl.I have a method which returns a concatenated string value (very large though) given a set of records. String generated inside that method is correct but when i am print the string again outside the method, perl is replacing "#" character with a non printable character (^M, ^C etc). Any idea why it would do this? Code wise here is snippet:
main () { my $string = foo(@arr); print "string outside function is " + $string; } foo() { //create string print "String inside function is " + $string; return $string; }
Output:

string inside function is "abc####123####-6####20111112 ..."

string outside function is "abc####123####-6###^M20111112 ..."

If you observe closely one # is replace by ^M character..Any help would be appreciated. Thanks

Replies are listed 'Best First'.
Re: Perl adding non printable characters in the return value
by Stamm (Sexton) on Nov 28, 2011 at 13:16 UTC
    In Perl, the concatenation operator is a dot (.), not a plus (+). In your code, Perl is trying to do a sum, maybe causing the garbled output. Try using a dot and see if it works.
Re: Perl adding non printable characters in the return value
by cdarke (Prior) on Nov 28, 2011 at 13:18 UTC
    ...and a Perl subroutine is defined with sub, but maybe your code is just a typo (looks like old ksh syntax).
    Anyway, one possibility might be that in one place you put the string inside quotes (interpolation) and the class is overloading stringification (see overload). You don't show that, but then again this obviously is not the whole code anyway.
Re: Perl adding non printable characters in the return value
by Anonymous Monk on Nov 28, 2011 at 12:33 UTC
    You are not showing the whole code. The question cannot be answered in a decent fashion if we cannot reproduce the problem.

    By the way, comments start with # in Perl, not //.

Re: Perl adding non printable characters in the return value
by ansh batra (Friar) on Nov 28, 2011 at 13:15 UTC
    have you tried printing the string INSIDE the function??
    "abc####123####-6####20111112 ..." is this what it printed?
    print "outside "+$str; #this prints "0"
    and if i remove sub keyword then it gives me compilation error
    so do confirm your output inside the func