This Week I Learned - 2016 Week 44

Last week post or the whole series.

Every time I reread Perl in about 2 hours 20 minutes, there is always new insights that increase my understanding of Perl itself. I can finally grok the intricacy of the three data types: scalar, array, and hash, especially the later two. To make it short, don't use list as container for arrays or hashes, just initialize and declare both types as anonymous array(using bracket) or hash (using braces). You can use them through reference or arrow (`->`) operator. Example as shown.
use v5.10;

my $contact = {
    name => 'John Doe',
    mobiles => [
        {carrier => 'at&t', no => '111-222-333'},
        {carrier => 't-mobile', no => '444-555-666'}
    ]
}

say $contact->{mobiles}->[0]->{carrier}; # at&t
say $contact->{mobiles}->[1]->{carrier}; # t-mobile

Another key concept is Perl calls by reference. This means when you pass a variable to a subroutine, it's a reference to the original value, not a copy. Any modification to the variable, will be reflected within the scope or context of the code until the end of the execution. The most important takeaway is that arguments to the subroutine is a list of scalar (yes, the @_ is a list) and the element of @_ is an alias to the passed parameters. According to perlsub documntation (emphasis added),
Therefore, if you called a function with two arguments, those would be stored in $_[0] and $_[1] . The array @_ is a local array, but its elements are aliases for the actual scalar parameters. In particular, if an element $_[0] is updated, the corresponding argument is updated (or an error occurs if it is not updatable). 

While we are on Perl (yes, we still talking about it). What are the preferred ways to check if an element exists in an array? Two most common way. One is using the default grep method, another is using the `List::MoreUtils` method.
# using grep
if (grep { $_ eq $element } @list) {
    ....
}

# using List::MoreUtils
use List::MoreUtils qw(any);
if ( any { $_ eq $element } @list ) {
    ....
}

My migration back to Vim from Sublime Text seems to be progressing quite well. It's nice to expose yourself to another editor and reapply certain features back to your default editor. First, is update tmux tab with Vim's opened file name. This was one of those things that you want to fix it, but never remember to do so. Next, fix copying from clipboard in Windows not working issue. Next, set indentation rules (like tab only) by file type. And lastly, how to open most recent opened files. Something I didn't realize exists in the default settings.

Remember Lorem Ipsum? Which is commonly used as filler text or place holder text in graphic design before the actual content is used. Well, this process is also known as Greeking. In Perl, there is this module, Text::Greeking, which provides such feature. There are others as well, like the usual Text::Lorem. But nothing can compare to the sheer bullshit of Lingua::ManagementSpeak which can generate meaningful sentences but pure bullshit management speak.

"a web browser is a JS interpreter". So true. Maybe I should start looking into all these "ancient" technologies (e.g. Tcl/Tk) instead of chasing latest greatest fad.

Is always a grey area if you choose to do development work in either porn or gambling industry. But I never realize that advertising industry is as shady as well.

Hate to be prompted for password every time you need to commit to Git repository through SSH? Save some typing by caching the credential. There are two ways.

Through SSH.
$ eval `ssh-agent -s`
$ ssh-add ~/.ssh/id_rsa_key
$ ssh-add -l
$ ssh git.example.com

Through Git.
$ git config --global credential.helper cache

No comments:

Post a Comment