Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
update Perl syntax; include forcing string values
Source Link
Wolf
  • 10.3k
  • 8
  • 72
  • 117

All answers, given so far, missed to mentionmentioning that the operator x does not only work on string literalsliterals, but also on string variables that are strings or expressions that buildevaluate to strings: like

use feature 'say'; my $msg = "hello "; printsay $msg x 2; printsay chr($msg33) x 2)3; 

like this

hello hello !!! 

and, even more important, x does an automatic conversion of expressions into strings if they aren't already (thanks to ggorlen for pointing me into that direction!). So for example

say 4 x 2; say [$msg] x 2; 

will result in something like the following as output

44 ARRAY(0x30ca10)ARRAY(0x30ca10) 

All answers, given so far, missed to mention that the operator x does not only work on string literals but also on string variables or expressions that build strings:

$msg = "hello "; print $msg x 2; print ($msg x 2) x 2; 

All answers, given so far, missed mentioning that the operator x does not only work on string literals, but also on variables that are strings or expressions that evaluate to strings like

use feature 'say'; my $msg = "hello "; say $msg x 2; say chr(33) x 3; 

like this

hello hello !!! 

and, even more important, x does an automatic conversion of expressions into strings if they aren't already (thanks to ggorlen for pointing me into that direction!). So for example

say 4 x 2; say [$msg] x 2; 

will result in something like the following as output

44 ARRAY(0x30ca10)ARRAY(0x30ca10) 
Source Link
Wolf
  • 10.3k
  • 8
  • 72
  • 117

All answers, given so far, missed to mention that the operator x does not only work on string literals but also on string variables or expressions that build strings:

$msg = "hello "; print $msg x 2; print ($msg x 2) x 2;