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

Hello,

I just fell over an error with concatinating strings with numbers. Here is a short testscript:

H:\>perl print "hello ".55." test\n"; String found where operator expected at - line 1, near "55." test\n"" (Missing operator before " test\n"?) ^Z syntax error at - line 1, near "55." test\n"" Execution of - aborted due to compilation errors. H:\>
If I write one or more spaces between 55 and the following dot, it is all ok. But this way, the dot after the 55 seems to be evaluated like a dot in a number like 55.23

Is this a bug or a feature?

Tested with the Perl-Versions:

Best regards,
perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

Replies are listed 'Best First'.
Re: Concatinating Numbers-Error: Bug or Feature
by BrowserUk (Patriarch) on Oct 29, 2002 at 11:06 UTC

    "55." is a legal float in Perl, so you need a space between the 55 and the . to disambiguate your intent.


    Nah! Your thinking of Simon Templar, originally played by Roger Moore and later by Ian Ogilvy
Re: Concatinating Numbers-Error: Bug or Feature
by mce (Curate) on Oct 29, 2002 at 11:23 UTC
    Hi,

    To avoid the concatination operator problem, use , in print and not ..
    What you wrote, is exactly the same as:

    print "hallo".55.0"test"; # <= error
    When you write it as:
    print "hallo" . 55 . "test";
    The perlparser (B::Deparse) will rewrite it as
    print 'hallo55test';
    Conclusion;
    print "hallo",55,"test";
    I hope this helps.
    ---------------------------
    Dr. Mark Ceulemans
    Senior Consultant
    IT Masters, Belgium
Re: Concatinating Numbers-Error: Bug or Feature
by busunsl (Vicar) on Oct 29, 2002 at 11:14 UTC
    I think it doesn't make any sense to concatenate a literal number to a string.
    You can put it inside the string as well and it would be faster.

    Apart from that it's pretty obvious that you have to put a space between the number and the dot to tell the parser what you want.

Re: Concatinating Numbers-Error: Bug or Feature
by gjb (Vicar) on Oct 29, 2002 at 19:48 UTC

    As a matter of fact, it would be safer to use printf since you'd end up with an empty string if the number happens to be zero.

    print "hello " . 0 . " test\n"; would result in hello  test which is probably not what you want, hence printf "hello %d test\n", (0); would be better since it produces hello 0 test.

    Hope this helps, -gjb-