Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Perl Concatenate vs Append Operator

by nande9 (Initiate)
on Aug 19, 2014 at 16:14 UTC ( [id://1097998]=perlquestion: print w/replies, xml ) Need Help??

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

I have a somewhat general question about the Perl concatenate operator and the append operator.

First, here is the example straight from Learning Perl (p.29)

# append a space to $str $str = $str . " ";
# same thing with assignment operator $str .= " ";

My question: Are either of these method more "correct" or preferred for speed or syntactical reasons?

Any information is greatly appreciated.

-- Nick

Replies are listed 'Best First'.
Re: Perl Concatenate vs Append Operator
by Your Mother (Archbishop) on Aug 19, 2014 at 17:21 UTC

    FWIW, there are better ways to do what concatenation does… 90% of the time? Not that it’s wrong but that there are cleaner ways that are more flexible and easier to build on or manipulate or test or pass around; print join " ", @strings; for example v $string .= $_ . " " for @stuff.

    There are no good or bad operators but, for me, string concatenation, while it is naturally the way a lot of think/code at first, is code smell most of the time.

Re: Perl Concatenate vs Append Operator
by Jim (Curate) on Aug 19, 2014 at 16:59 UTC

    Neither is more correct than the other. TMTOWTDI, that's all.

    I prefer to use the assignment operators because I think they're more intuitive and more idiomatic. And I prefer auto-increment and auto-decrement to the assignment operators. So I'd do this…

    $total_days++;

    …instead of this…

    $total_days += 1;

    …and I'd never do this…

    $total_days = $total_days + 1;

    UPDATE:  I concur with Your Mother's remarks about the concatenation operator. I hardly ever use it. In most cases, there's a better way to accomplish one's string-building tasks using other methods, particularly join().

Re: Perl Concatenate vs Append Operator
by McA (Priest) on Aug 19, 2014 at 17:31 UTC

    Hi,

    just as an addition. Compare the output of:

    perl -MO=Terse -E'my $str = "McA"; $str = $str . " ";'

    and

    perl -MO=Terse -E'my $str = "McA"; $str .= " ";'

    So the internal way of handling this (tried with Perl 5.10.1) is the same.

    Regards
    McA

      But!

      perl -MO=Terse -E'our $str; $str = $str . " ";'
      and
      perl -MO=Terse -E'our $str; $str .= " ";'

        Hi,

        I'm so used to lexical variables that I didn't think about that. That's really interesting.

        Thank you for that insight.

        McA

      I just tried it on Perl 5.14, I do not get exactly the same thing with your two one-liners:
      $ perl -MO=Terse -E'my $str = "McA"; $str = $str . " ";' LISTOP (0x600153910) leave [1] OP (0x60007fdb0) enter COP (0x60006e3c0) nextstate BINOP (0x6001f1ea0) sassign SVOP (0x60006e2e0) const [3] PV (0x6001f2c08) "McA" OP (0x60006f890) padsv [1] COP (0x6001f7fe0) nextstate BINOP (0x6001f7fa0) concat [1] OP (0x600153980) padsv [1] SVOP (0x6001f7f60) const [4] PV (0x6001f2b78) " " -e syntax OK $ perl -MO=Terse -E'my $str = "McA"; $str .= " ";' LISTOP (0x600153910) leave [1] OP (0x60007fdb0) enter COP (0x60006e3c0) nextstate BINOP (0x6001f1ea0) sassign SVOP (0x60006e2e0) const [3] PV (0x6001f2c08) "McA" OP (0x60006f890) padsv [1] COP (0x6001f7f60) nextstate BINOP (0x6001539c0) concat [2] OP (0x600153950) padsv [1] SVOP (0x600153980) const [4] PV (0x6001f2b78) " " -e syntax OK
      There are some differences on the ante-penultimate and penultimate lines. But I am not really able to assess whether the difference is significant. The Deparse module also gives different output for these two lines in Perl 5.14.
Re: Perl Concatenate vs Append Operator
by Anonymous Monk on Aug 19, 2014 at 18:55 UTC
    I would be surprised if it did not translate to essentially the same set of internal interpreter-instructions.

      A little late to the party? McA and another anon already showed the appropriate commands more than 45 minutes before your post.

        In other words ... "yes." As any sensible compiler design team would have known to do, and did.
Re: Perl Concatenate vs Append Operator
by Anonymous Monk on Aug 19, 2014 at 17:10 UTC

    As far as I kn(o|e)w, Perl ha(s|d) only concatenate operator. There (i|wa)s no append operator.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-25 22:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found