I just updated this article in 2011 to update it to the current Perl "best practices". I hope these Perl string concatenation examples have been helpful. I rarely use the Perl join function to merge simple strings like this, but I thought I'd show this as another possible way to concatenate Perl strings. The code shown in Listing 2 below uses the Perl join function to achieve the same result as the code shown in Listing 1: With the join function you can merge as many Perl strings together as you like, and you can specify what token you want to use to separate each string. Perl string concatenation - Method #3 - using Perl's join functionĪnother way to create the desired variable $filename is to use the Perl join function. For example x + y is always a numeric addition, and if x or y do not contain numbers, an attempt is made to convert them to numbers first. # $filename now contains "/tmp/checkbook.tmp"Īgain, this example creates a Perl variable named $filename that contains the concatenated string contents shown on the right side of that statement. In Perl, the operator determines what operation is performed, independent of the type of the operands. To concatenate Perl strings this way, just place the dot operator in between the strings you want to merge on the right side of the '=' symbol in your statement, as shown here: In some circumstances it can make your Perl script easier to read than the syntax shown above. Perl string concatenation - Method #2 - using Perl's dot operatorĪnother way to concatenate Perl strings is to use the "dot" operator (i.e., the decimal character). You might be thinking: If strings and integers are equivalent in Perl, why do we need two kinds of comparison. These days I use this Perl string concatenation approach more than any other. String Comparison Versus Integer Comparison. This example prepends the string '/tmp/' to the variable $name, and appends the '.tmp' filename extension to $name. In this example, I have a Perl string variable named $name that contains the text "foo", and I use this Perl syntax to embed the value of that variable in the string that I'm assigning to the new variable $filename. One of the best and simplest ways to concatenate Perl strings is with this syntax: Perl string concatenation - Method #1 - Using $ After getting the first part of the filename, I wanted to prepend the directory "/tmp/" to the beginning of the name, and append the filename extension ".tmp" to the end of the name. I was going to get the first part of the filename (the filename prefix) by calling a function that generated temporary filenames. The Dot (.) sign is used instead of (+) sign in Perl. For instance, I recently had a need to create a temporary filename, and wanted to use the original filename as part of the temporary name. There are different types of string operators in Perl, as follows: Concatenation Operator (.) Repetition Operator (x) Auto-increment Operator (++) Concatenation Operator (.) Perl strings are concatenated with a Dot (.) symbol. When you work with Perl, you're often working with strings, and very often you need to concatenate strings. Perl string FAQ: How do I concatenate Perl strings?
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |