Updated April 1, 2023
Introduction to Perl trim
Perl trim removes the leading and trailing unwanted space of the Perl string. The trim is to cut the starting and ending whitespace of the Perl string and save the memory. Trim removes unnecessary white space of the Perl string and stores the required data in the database. The trim is useful to remove the space which is occurred before the initial and final character of the string. The trim is useful when the string wanted to cut both ends of unwanted space and save the necessary data.
Syntax
The basic trim syntax is below.
$ perl_trim_variable =~ s/^\s+|\s+$//;
- This syntax is used to clear both sides of the white space of the string.
- The basic Perl syntax used a regular expression to cut the leading and trailing unwanted space of the string.
The left side trim syntax is below.
$ left_perl_trim_variable =~ s/^\s+//;
- This syntax is used to clear the left side of the white space of the string.
- The left side Perl syntax used a regular expression to cut the leading unwanted space of the Perl string.
The right-side trim syntax is below.
$ right_perl_trim_variable =~ s/\s+$//;
- This syntax is used to clear the right side of the white space of the string.
- The right-side syntax used a regular expression to cut the trailing space of the Perl string.
How does the trim function work in Perl?
- To download the Perl software and install it in your operating system of the device.
https://www.Perl.org/ or http://strawberryPerl.com/ are commonly using software websites.
- Make a file with the Perl extension in the device and save the file in the command line path.
Example:
helloo.pl or first pearl.pl
- Create the string variable and Initialize with value for the trim.
$ perl_trim_function = " Perl trim ";
- Use required trim syntax on the given Perl String variable.
$ perl_trim_function =~ s/^\s+|\s+$//;
- Return the String after using trim syntax.
print " Perl trim string is below. \n ";
print" $ perl_trim_function \n ";
- The combined working step of trim is below.
$ perl_trim_function = " Perl trim ";
print " original string is below. \n ";
print" $ perl_trim_function \n ";
$ perl_trim_function =~ s/^\s+|\s+$//;
print " Perl trim string is below. \n ";
print" $ perl_trim_function \n ";
Examples
Here are the following examples mentioned below.
Example #1
The basic trim example and Output.
Code:
$ perl_trim_function = " Perl trim ";
print " original string is below. \n ";
print" $ perl_trim_function \n ";
$ perl_trim_function =~ s/^\s+|\s+$//;
print " Perl trim string is below. \n ";
print" $ perl_trim_function \n \n ";
$ perl_trim_function1 =" 9, 8, 7, 6, 5 ";
print " original value is below. \n ";
print" $ perl_trim_function1 \n ";
$ perl_trim_function1 =~ s/^\s+|\s+$//;
print " Perl trim value is below. \n ";
print" $ perl_trim_function1 \n \n \n ";
$ perl_trim_function2 = " Perl trim, 1, 2 ";
print " original value is below. \n ";
print" $ perl_trim_function2 \n ";
$ perl_trim_function2 =~ s/^\s+|\s+$//;
print " Perl trim value is below. \n ";
print" $ perl_trim_function2 \n \n \n";
$ perl_trim_function3 = " @, #, $, %, & ";
print " original value is below. \n ";
print" $ perl_trim_function3 \n ";
$ perl_trim_function3 =~ s/^\s+|\s+$//;
print " Perl trim value is below. \n ";
print" $ perl_trim_function3 \n \n \n";
Output:
Example #2
The left side trim example and Output.
Code:
$ perl_trim_function = " left side Perl trim ";
print " original string is below. \n ";
print" $ perl_trim_function \n ";
$ perl_trim_function =~ s/^\s+//;
print " left side Perl trim string is below. \n ";
print" $ perl_trim_function \n \n ";
$ perl_trim_function1 =" 9, 8, 7, 6, 5 ";
print " original value is below. \n ";
print" $ perl_trim_function1 \n ";
$ perl_trim_function1 =~ s/^\s+//;
print " left side Perl trim value is below. \n ";
print" $ perl_trim_function1 \n \n \n ";
$ perl_trim_function2 = " left side Perl trim, 1, 2 ";
print " original value is below. \n ";
print" $ perl_trim_function2 \n ";
$ perl_trim_function2 =~ s/^\s+//;
print " left side Perl trim value is below. \n ";
print" $ perl_trim_function2 \n \n \n";
$ perl_trim_function3 = " @, #, $, %, & ";
print " original value is below. \n ";
print" $ perl_trim_function3 \n ";
$ perl_trim_function3 =~ s/^\s+//;
print " left side Perl trim value is below. \n ";
print" $ perl_trim_function3 \n \n \n";
Output:
Example #3
The right side trims example and Output.
Code:
$ perl_trim_function = " right side Perl trim ";
print " original string is below. \n ";
print" $ perl_trim_function \n ";
$ perl_trim_function =~ s/\s+$//;
print " right side Perl trim string is below. \n ";
print" $ perl_trim_function \n \n ";
$ perl_trim_function1 =" 9, 8, 7, 6, 5 ";
print " original value is below. \n ";
print" $ perl_trim_function1 \n ";
$ perl_trim_function1 =~ s/\s+$//;
print " right side Perl trim value is below. \n ";
print" $ perl_trim_function1 \n \n \n ";
$ perl_trim_function2 = " right side Perl trim, 1, 2 ";
print " original value is below. \n ";
print" $ perl_trim_function2 \n ";
$ perl_trim_function2 =~ s/\s+$//;
print " right side Perl trim value is below. \n ";
print" $ perl_trim_function2 \n \n \n";
$ perl_trim_function3 = " @, #, $, %, & ";
print " original value is below. \n ";
print" $ perl_trim_function3 \n ";
$ perl_trim_function3 =~ s/\s+$//;
print " right side Perl trim value is below. \n ";
print" $ perl_trim_function3 \n \n \n";
Output:
Example #4
The trim uses an array example and output.
Code:
@perl_trim_function = (" basic "," perl "," trim ");
print " original string is below. \n ";
print" @perl_trim_function \n ";
@perl_trim_functions = grep(s/^\s+|\s+$//g, @perl_trim_function);
print " Perl trim string is below. \n ";
print" @perl_trim_functions \n \n ";
@perl_trim_function = (" Left "," side "," perl "," trim ");
print " original string is below. \n ";
print" @perl_trim_function \n ";
@perl_trim_functions = grep(s/^\s+//g, @perl_trim_function);
print " left side Perl trim string is below. \n ";
print" @perl_trim_functions \n \n ";
@perl_trim_function = (" right "," side "," perl "," trim ");
print " original string is below. \n ";
print" @perl_trim_function \n ";
@perl_trim_functions = grep(s/\s+$//g, @perl_trim_function);
print " right side Perl trim string is below. \n ";
print" @perl_trim_functions \n \n ";
Output:
Description:
- The grep() method is used for array elements and trim as per users requirements.
- The g operator is used for the global variable and chooses all elements for trim.
Conclusion
Trim is useful for removing the whitespace of the string in Perl technology. The trim is saving the memory space of the database and removes unnecessary spaces of the Perl string.
Recommended Articles
This is a guide to Perl trim. Here we discuss How does the trim function works in Perl and Examples along with the codes and outputs. You may also have a look at the following articles to learn more –