Single quote vs double quote in perl

The double-quoted string will have its escape sequences processed, and the single-quoted string will not. #!/usr/bin/perl use warnings; print ‘\tThis is a single-quoted string.\n’; print "\tThis is a double-quoted string.\n"; The output is \tThis is a single-quoted string.\n This is a double-quoted string.
Continue reading…

Enjoyed this post? Share it!

 
 

Perl Right trim function to remove trailing whitespace

sub rtrim($) { my $string = shift; $string =~ s/\s+$//; return $string; } Example: # Create a test string my $string = " \t Hello world! "; # Here is how to output the trimmed text “Hello world!” print rtrim($string)."\n";
Continue reading…

Enjoyed this post? Share it!

 
 

Strip out leading whitespace in perl

The code in the example removes leading whitespace from the text of the here document. The /m modifier lets the ^ character match at the start of each line in the string, and the /g modifier makes the pattern matching engine repeat the substitution as often as it can (i.e., for every line in the […]
Continue reading…

Enjoyed this post? Share it!

 
 

Interpolating functions and expressions in perl strings

We can construct more complex templates with scalar variable and function interpolation. Developers generally want a function call or expression to expand within a string. You can break up your expression into distinct concatenated pieces: $endresult = $varone . funccall() . $vartwo; # scalar only Or you can use @{[ LIST EXPR ]} or ${ […]
Continue reading…

Enjoyed this post? Share it!

 
 

Convert uppercase string to lowercase in Perl

We can use the lc and uc functions or the \L and \U string escapes to convert between upper and lower case in Perl. use locale; # needed in 5.004 or above   $upper = uc($lower); # "hello" -> "HELLO" $lower = lc($upper); # "HELLO" -> "hello" $upper = "\U$lower"; # "how" -> "HOW" $lower […]
Continue reading…

Enjoyed this post? Share it!

 
 

Split string into characters in perl

We can process a string one character at a time by splitting it using the split function and a null parameter defined as // $string = "w3m"; @strarr = split(//, $string); Output: strarr[0] = w strarr[1] = 3 strarr[2] = m We can also use a loop to extract characters as while ($string =~ /(.)/g) […]
Continue reading…

Enjoyed this post? Share it!

 
 

Using unpack function in perl

The unpack function gives only read access, but is faster when you have many substrings to extract. # get a 5-byte string, skip 3, then grab 2 8-byte strings, then the rest ($leading, $s1, $s2, $trailing) = unpack("A5 ×3 A8 A8 A*", $data);   # split at five byte boundaries @fivers = unpack("A5" × (length($string)/5), […]
Continue reading…

Enjoyed this post? Share it!

 
 

Get substring from string using perl

$string = "This is what you have"; # +012345678901234567890 Indexing forwards (left to right) # 109876543210987654321- Indexing backwards (right to left)   $first = substr($string, 0, 1); # "T" $start = substr($string, 5, 2); # "is" $rest = substr($string, 13); # "you have" $last = substr($string, -1); # "e" $end = substr($string, -4); # "have" […]
Continue reading…

Enjoyed this post? Share it!

 
 

Access or modify substring using Perl

If developers want to access or modify just a portion of a string, the substring function in perl can be used. An example usage would be to read a fixed-width record and to extract the individual fields. The substr function lets you read from and write to bits of the string. $value = substr($string, $offset, […]
Continue reading…

Enjoyed this post? Share it!

 
 

Here docs in perl

”Here documents” are a way to quote a large chunk of text. The text can be interpreted as single-quoted, double-quoted, or even as commands to be executed, depending on how you quote the terminating identifier. Here we double-quote two lines with a here document: $str = <"EOF"; This is a multiline here document terminated by […]
Continue reading…

Enjoyed this post? Share it!

 
 

Quote operators in perl

We can specify strings in a perl program either with single quotes, double quotes using the quote-like operators q// and qq//, or “here documents.” $string = q/Jon ‘Main’ Deva/; # literal single quotes You can use the same character as delimiter, as we do with / here, or you can balance the delimiters if you […]
Continue reading…

Enjoyed this post? Share it!