Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

simple scalars question

by mitchreward (Acolyte)
on Jul 18, 2014 at 09:32 UTC ( [id://1094190]=perlquestion: print w/replies, xml ) Need Help??

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1094190]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2026-04-17 18:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.