Match a string using regular expressions in Perl
Answer:
The following code demo how to match a string in Perl,
my $s = 'love foobar';
if ( $s =~ /foo/ ) {
print 'matched';
}
Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.
Match a string using regular expressions in Perl
Answer:
The following code demo how to match a string in Perl,
my $s = 'love foobar';
if ( $s =~ /foo/ ) {
print 'matched';
}
Trim a string using Perl
Answer:
To trim a string in Perl, use the following subroutine - trim():
#!/usr/bin/perl
use strict;
sub trim($) {
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
print trim (' foo ');
Search and replace string in file using Perl
Answer:
Besides using sed, Perl can also replace file in place with one-liner.
# perl -0777 -i -pe 's/abc/def/g' test.txt
Install Perl module using CPAN
Answer:
CPAN provide a very easy way to install Perl modules in Linux system
# cpan -i 'Digest::MD5'
The above command will install the module "Digest::MD5" into the system.
Disable output buffering in Perl
Answer:
To disable output buffering in Perl, add the following line in the Perl's code.
$| = 1;
That's all.