Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Typo or on purpose? Variable instantiation with string concatenation operator

by ikegami (Patriarch)
on Oct 06, 2015 at 15:23 UTC ( [id://1143934]=note: print w/replies, xml ) Need Help??


in reply to Typo or on purpose? Variable instantiation with string concatenation operator

my $str1 .= $self->sanitizeString($input1);

will do the same thing as

my $str1 = $self->sanitizeString($input1);

but it makes no sense to use the former.

The benchmarks show no difference in speed. (Anything under 1% is definitely meaningless. I question anything under 5%. I used kennethk's code, but changed timethese to cmpthese to produce more useful output, and changed the label to something meaningful.)

Rate concat normal concat 771935/s -- -1% normal 779433/s 1% --

Replies are listed 'Best First'.
Re^2: Typo or on purpose? Variable instantiation with string concatenation operator
by Crackers2 (Parson) on Oct 06, 2015 at 18:07 UTC
    Not relevant for this specific case, but there is a difference if the thing being appended is a number:
    $ perl -e'use Devel::Peek; my $x = 5; Dump($x) ; my $y .= 5; Dump($y); +' SV = IV(0x2fc84) at 0x2fc88 REFCNT = 1 FLAGS = (PADMY,IOK,pIOK) IV = 5 SV = PV(0x13838) at 0x2fcd8 REFCNT = 1 FLAGS = (PADMY,POK,pPOK) PV = 0x2ac18 "5"\0 CUR = 1 LEN = 12

    i.e. .= will stringify on assigment.

      Good point. I'd use the following if I wanted to force stringification:
      my $str1 = "".$self->sanitizeString($input1);

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2024-03-28 10:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found