Howo to get OS type using Perl?
Answer:
The $^O variable contains an information of the operating system that your perl binary was built for.
E.g.
# perl -e 'print $^O;'
linux
Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.
Howo to get OS type using Perl?
Answer:
The $^O variable contains an information of the operating system that your perl binary was built for.
E.g.
# perl -e 'print $^O;'
linux
Playing with qw() function in Perl
Answer:
The qw (quote word) function is very useful to generate a list of words.
E.g.
my @names = ('peter', 'john', 'mark');
print @names;
my @names2 = qw(peter john mark);
print @names2;
As you can see above, the use of qw() made the code more readable.
Read command line arguments in Perl
Answer:
To read the command line arguments in Perl, you need to play with the special array $ARGV
E.g. test.pl
#!/usr/bin/perl
use strict;
print $ARGV[0];
Then execute
# perl test.pl hello
hello
The first argument will be printed to the screen.
How to print current Unix timestamp
Answer:
Using Perl,
perl -e 'print time;'
How to replace a string in a file using Perl
Answer:
Assume you have a file "test'txt", contains the string "abc", and you want to replace by "def", use the following script
perl -pi -e "s/abc/def/g;" test.txt