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

hi guys,

Why is the following code print the \n first before the scalars ?

I wanted it like this, print $toto, => new line => print $tutu ...

#!/usr/bin/perl -w use strict; use warnings; my $toto = 'toto'; my $tutu = 'tutu'; my $deux = $toto, print "\n", $tutu; print $deux

Replies are listed 'Best First'.
Re: simple scalars question
by AppleFritter (Vicar) on Jul 18, 2014 at 10:03 UTC

    Hello again, Mitch! To expound on my anonymous brother's answer, what you're doing here:

    my $deux = $toto, print "\n", $tutu;

    is assigning $toto to $deux, and then executing print "\n", $tutu. In order to embed a newline in $deux, you could do either of the following:

    $deux = "$toto\n$tutu"; # variable interpolation in double-quot +ed strings $deux = $toto . "\n" . $tutu; # string concatenation

    Of course, if you merely care about printing, the following will also work:

    print $toto, "\n", $tutu;

    This works because print is a list operator that can take an arbitrary number of arguments -- but you can't do the same when assigning to a scalar, you have to use either interpolation or string concatenation (the . operator).

Re: simple scalars question
by Anonymous Monk on Jul 18, 2014 at 09:35 UTC
    Because you wrote
    my $deux = $toto, print "\n", $tutu;
    and you did not write
    print $toto, "\n", $tutu;
      ok, that's it. thank you