You can do single-line commands through:
perl -e 'single-line command separated with semicolon'
To print Hello World! with Perl:
perl -e 'print "Hello World!\n"; print "How are you today?\n"'
A Perl script should have a shbang reference to the Perl location:
#!/usr/bin/perl
After the shbang reference, you can begin your scripting:
print "Hello World!\n";
print "How are you today?", "\n";
Notice that concatenation is done through the comma ",".
To execute a Perl script, there are two things that can be done. We can use the Perl interpreter:
perl test.pl
Or you can change the execute permission:
chmod u+x perl.pl
Note that the shbang reference is very important for directly executing the file or else the Bash shell would not know that it's a Perl script.
You can pass arguments (parameters) into a Perl script. Arguments are stored in the $ARGV[x] array. The first argument starts with $ARGV[0]:
print "Your first argument is $ARGV[0]"
To pass in arguments:
./test.pl Hello!
Arguments are delimited by spaces. The following example will have Hello in $ARGV[0] and World in $ARGV[1]:
/test.pl Hello World
Perl has the best support for REGEXP. To do a REGEXP match:
if ($ARGV[0] =~ //)
{
print "$ARGV[0]\n";
}
You can check for compilation errors through:
perl -c test.pl
No comments :
Post a Comment