To-Study Plan: a beginning to OO in Perl

Now I have a todo list command-line script. I want to have a to-do list for my wikipedia editing activities, a to-study list for math I want to study sparely, a to-study-or-to-code in computer programming and maybe a to-read list for books.

The current format of the todo list script is roughly like this:

C:\Users\user> h

Command-line Hacker's Todo List, v0.1, GNU gpl-3.0; I work with a file "hackerstodo.txt" on the destination location. a : add new item t 2 : make the second item become the first priority d 3 : delete the 3rd item : display the TODO list b 4 : put the 4th item to the bottom pop : announe that you have finished the first task! clear : clear all items in TODO list h : display this help message that you are reading

C:\Users\user> t 2

1. update blog

2. Draft an email on Review and feedback to XXX

3. send the email on Review and feedback to XXX

4. draft an email to Mr Lo

5. send the email to Mr Lo

6. solve older Perl Weekly Challenges

And the codes of is currently:
# For Strawberry Perl under Windows System
# - Command-line Hacker's Todo List, v0.1, GNU gpl-3.0;
# I work with a file "hackerstodo.txt".
# Setup:
# 1. Put within the folder
# having perl.exe, which is C:\Strawberry\perl\bin
# on Strawberry Perl Windows.
# 2. create a text file on a folder which does not require system admin authority, write your list of to-do stuff inside it
# 3. modify the value of the variable $hackstodo on line 21
# Author: FUNG Cheok Yin <> or
# <>
# Mar 07, 2020
use strict;
use Switch;
use File::Copy;
use Cwd;

my $hackstodo = 'C:\D\hackerstodo.txt';  
# Do not set this file in a system folder
# or you will probably get "Access denied"

my $listofparameters = qq{
Command-line Hacker\'s Todo List
... h     \: display this help message that you are reading

my $originaldir = getcwd();
if (-e 'C:\Strawberry\perl\bin\hackerstodo~temp') {system 'del C:\Strawberry\perl\bin\hackerstodo~temp'};
if (-e $hackstodo) {
        copy($hackstodo, 'C:\Strawberry\perl\bin\hackerstodo~temp');
    system "del $hackstodo";
} else {
    system "echo > $hackstodo";
open LOG, 'C:\Strawberry\perl\bin\hackerstodo~temp';

my @list = ();

# get todo list from the text file $hackstodo
foreach (<LOG>) {
    {push @list, $_ unless $_ =~ /^\s*$/; }

# begin of subroutines

sub clear {
    copy $hackstodo ,'C:\Strawberry\perl\bin\todo-backup';
           @list = ();}

sub display {print "\n"; foreach (0..$#list) {print(($_+1), ". " , $list[$_], "\n") ; } }

sub suattop {my $top; $top = splice @list, $_[0]-1, 1; unshift @list, $top ;}

sub del {splice @list, $_[0]-1, 1;}

sub helpmsg {print $listofparameters;}

sub add {chomp($_ = <STDIN>); push @list, $_ unless $_ =~ /^\s*$/;}

sub bottom {my $temp = splice @list, $_[0]-1, 1; push @list, $temp;}

# end of subroutines

# handle the arguments
switch($ARGV[0]) {
    case "clear" {clear; print "The todo list is empty now."}
    case "t" {suattop($ARGV[1]); display}
    case "pop" {shift @list; print "Congratulations for finishing a task today!\n \n * * * * * * * * \nLatest TODO List \n"; display}
    case "a" { print "Enter new items:"; &add; print "\n"; &display}
    case "d" {del($ARGV[1]); display}
    case "b" {bottom($ARGV[1]); display}
    case "h" {helpmsg}
    else {display}

close LOG;

system 'del C:\Strawberry\perl\bin\hackerstodo~temp' if -e 'C:\Strawberry\perl\bin\hackerstodo~temp';
system "echo > $hackstodo";
open LOG2, ">$hackstodo" or die "wrong here, LOG2";

foreach (@list) {print LOG2 ($_, "\n");}
close LOG2;
chdir $originaldir;

Now, it seems that "object-oriented-ize"/"package-size" the script can satisfy my needs on what I said in the beginning of this post.

In Perl, there are several choices for OO. There is a built-in OO system; and there is Moose. I come across Moo also.

I am going to blog what I have learnt here. One-line background: I know some C++ but I don't know OO. The rest of March will be busy, but in April there are some public holidays; I expect I can update the progress in April. (Firstly, modify the indention! However, I find that the Rich Text Editor on Movable Type automagically converts the tabs. Then...)


Before you jump in Moose and friends, I suggest you try it with straight Perl OO first. You can learn the bare metal approach so when you jump into another framework, you appreciate what it's doing for you.

Damian Conway's Object-oriented Perl is a very good book for not only learning some Perl, but the basics of object orientation. (Whereas Intermediate Perl is mostly good for just the Perl and not the theory).

I don't know if I quite agree with this. Following this logic, before implementing anything in Perl, you should try it with C or perhaps even assembly language, so you appreciate what Perl is doing for you.

Moo and Moose abstract away a lot of the tedious parts of Perl's OO, but like all abstractions, sometimes the abstraction leaks. Sometimes you're doing something slightly unusual and you need to deal with the guts that Moo and Moose are trying to cover up.

But I really think it's fine to cross that bridge when you come to it. I don't think using Moo or Moose as a starting point is a bad idea. Different people have different approaches to learning; starting at the ground level might work better for some than it does for others.

Leave a comment

About C.-Y. Fung

user-pic This blog is inactive and replaced by ; but I post highly Perl-related posts here.