tags: Perl 5
It’s been a while since I could enjoy daily programming life with Perl 5.
I tried to find a chance to make any program with Perl, but it’s not easy especially when co-workers does not looks very happy reading the Perl code.
There are many languages showing off their fancy features and paradigm, like Kotlin and Haskell or even new projects on going with OpenJDK, however I believe perl is still a very powerful tool for any kind of utilization.
The purpose of posts regarding the perl is mostly like a memo to myself.
bash
perldoc -m MeCab
perldoc -m Try::Tiny
perl perlfaq perltoc perldiag perldoc CGI
-D : Describes search for the item in detail
-q : perldoc -q "something I'm looking for" perlfaq-search-regexp
-l : perldoc -l Time::HiRes Display only the file name of the module found
Type > perldoc perldoc for details
The third line of below code block is to tell perl to use env program to find out which perl is set as the default perl on the system.
#!/usr/bin/perl
#!/usr/local/bin/perl
#!/usr/bin/env perl
bash
$ perl -V
$ perl -le "print for @INC"
perldoc -l Time::HiRes # see location
perl -MTime::HiRes -e 'print $INC{"Time/HiRes.pm"}.\n'
Following code doesn’t work becuase unshift is executed at runtime and the use pragma evaluated at compile time.
unshift @INC, '/Users/wonha/lib';
use Navigation::SeatOfPandts;
Correct way is to use BEGIN block :
BEGIN {unshift @INC, '/Users/gilligan/lib'; }
use Navigation::SeatOfPandts;
perl
($dino) = something;
@arr = ( );
my keyword declares lexical variable, which is same as private varibale of that scopestate is similar to final keyword in Java
perl
use feature 'state'
> perldoc perlvar
| VARIABLE | DESCRIPTION |
|---|---|
| @_ | a |
| @INC | Contains paths to look for files loaded with do, require, or use. |
| %INC | Contains entries for every file loaded with do, require, or use. |
| $^V | The current Perl version |
| $^X | The executable used to execute your program |
| $@ | Perl syntax error trapped by eval. |
| $/ | input record separator (defaultly, newline) |
| $ | output record separator |
| $” | list separator ( print “@array\n”;) |
| $! | contain system call failure reason |
| $l | set this to 1 will set the currently selected filehande(by select operator) buffer flushed after each output operation |
| %ENV | . |
| $$ | current process ID |
perl
my $letter = <<"SQL";
SELECT c.id
FROM customers c
JOIN orders o On c.id = customer_id
SQL
my %french_for = qw/1 un 2 deux 3 trois/;
###
my %perl = map { $_, 1 } @required;
for my $item (@required) {
if ( $perl{$item} ) {
print "perl have the item\n";
}
}
###
@arry = %people = ( %classmate, austen => 'Jane', Lincoln => "Abraham" );
###
my(undef, undef, undef, undef, $mtime, $ctime) = stat $some_file # this is annoying
my ($ctime1, $mtime, $ctime2) = (stat $some_file)[5,4,5]; # use a list slice
###
while ( ( $index, $value ) = each @rocks ) {
print "$index: $value\n";
}
###
my @fields = split /:/, ":::a:b:c:::"; # gives ("", "", "", "a", "b", "c")
sub F { my $n = shift; return 0 if $n == 0; return 1 if $n == 1; return F($n-1) + F($n-2); } print F(7); ```