Perl 6 中的 IO 操作

批量插入文本


use v6;

my @filenames = dir '.',  test => any(/\.md$/, /\.markdown/);

for @filenames -> $filePath {
    my $path = $filePath.path();
    $path ~~ s/.md//;
    $path ~~ s/.markdown//;

    my $date = DateTime.new(now);
    my $head =
qq:heredoc 'EOT';
title:  $path.IO.basename()
date: $date
tags: Perl6
categories: Perl 6

---

<blockquote class="blockquote-center">這城市有太多風(fēng)景都在提醒那過去狈谊!</blockquote>

[TOC]

EOT

   my @content   = slurp $filePath;
   spurt($filePath.path, "$head\n@content[]");
}

在當(dāng)前目錄中查找所有以 .md (.markdown)結(jié)尾的文件(即markdown文件), 并在文件最前面插入一段文本赂毯, 形如:

title:  Perl6
date: 2015-08-20T23:19:13Z
tags: Perl6
categories: Perl 6

---

<blockquote class="blockquote-center">我站在天橋上念你, 有點狼狽</blockquote>

IO::Path 提供了 basename, path, parts, 等方法供使用, 具體用法請看文檔:

p6doc IO::Path

一些例子:

say IO::Path.new("/etc/passwd").basename;   # passwd
say IO::Path.new("docs/README.pod").extension;   # pod
say IO::Path.new("/etc/passwd").dirname;    # /etc
say IO::Path::Win32.new("C:\\Windows\\registry.ini").volume;    # C:
say IO::Path.new("/etc/passwd").parts.perl # ("dirname" => "/etc", "volume"  => "", "basename" => "passwd").hash

Examples:



    # To iterate over the contents of the current directory: for dir() -> $file
    {
        say $file;
    }

    # As before, but include even '.' and '..' which are filtered out by # the
    default :test matcher: for dir(test => *) -> $file {
        say $file;
    }

    # To get the names of all .jpg and .jpeg files in ~/Downloads: my @jpegs =
    "%*ENV<HOME>/Downloads".IO.dir(test => /:i '.' jpe?g $/)?.Str;


# An example program that lists all files and directories recursively:

    sub MAIN($dir = '.') {
        my @todo = $dir.IO; while @todo {
            for @todo.pop.dir -> $path {
                say $path.Str; @todo.push: $path if $path.d;
            }
        }
    }

文件測試操作符


# If you have a string - a path to something in the filesystem:

    if "path/to/file".IO ~~ :e {
        say 'file exists';
    }

    my $file = "path/to/file"; if $file.IO ~~ :e {
        say 'file exists';
    }

# Instead of the colonpair syntax, you can use method calls too:

    if 'path/to/file'.IO.e {
        say 'file exists';
    }

# If you already have an IO object in $file, either by creating one yourself, or
# by getting it from another subroutine, such as dir, you can write this:

    my $file = "path/to/file".IO; if $file ~~ :e {
        say 'file exists';
    }

文件時間戳:


say "path/to/file".IO.modified;                # e.g. Instant:1424089165
say DateTime.new("path/to/file".IO.modified);  # e.g. 2015-02-16T12:18:50Z

my $modification_instant = "path/to/file".IO.modified;
my $modification_time    = DateTime.new($modification_instant);
say $modification_time;  # e.g. 2015-02-16T12:18:50Z


say "path/to/file".IO.accessed;                # e.g. Instant:1424353577
say DateTime.new("path/to/file".IO.accessed);  # e.g. 2015-02-19T13:45:42Z

my $access_instant = "path/to/file".IO.accessed;
my $access_time    =  DateTime.new($access_instant);
say $access_time;  # e.g. 2015-02-19T13:45:42Z


say "path/to/file".IO.changed;                # e.g. Instant:1424089165
say DateTime.new("path/to/file".IO.changed);  # e.g. 2015-02-16T12:18:50Z

my $change_instant = "path/to/file".IO.changed;
my $change_time    =  DateTime.new($chnge_instant);
say $change_time;  # e.g. 2015-02-16T12:18:50Z
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末厅各,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子斯嚎,更是在濱河造成了極大的恐慌土陪,老刑警劉巖光坝,帶你破解...
    沈念sama閱讀 222,183評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異忽孽,居然都是意外死亡绑改,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評論 3 399
  • 文/潘曉璐 我一進店門兄一,熙熙樓的掌柜王于貴愁眉苦臉地迎上來厘线,“玉大人,你說我怎么就攤上這事出革≡熳常” “怎么了?”我有些...
    開封第一講書人閱讀 168,766評論 0 361
  • 文/不壞的土叔 我叫張陵骂束,是天一觀的道長费薄。 經(jīng)常有香客問我,道長栖雾,這世上最難降的妖魔是什么楞抡? 我笑而不...
    開封第一講書人閱讀 59,854評論 1 299
  • 正文 為了忘掉前任,我火速辦了婚禮析藕,結(jié)果婚禮上召廷,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好竞慢,可當(dāng)我...
    茶點故事閱讀 68,871評論 6 398
  • 文/花漫 我一把揭開白布先紫。 她就那樣靜靜地躺著,像睡著了一般筹煮。 火紅的嫁衣襯著肌膚如雪遮精。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,457評論 1 311
  • 那天败潦,我揣著相機與錄音本冲,去河邊找鬼。 笑死劫扒,一個胖子當(dāng)著我的面吹牛檬洞,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播沟饥,決...
    沈念sama閱讀 40,999評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼添怔,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了贤旷?” 一聲冷哼從身側(cè)響起广料,我...
    開封第一講書人閱讀 39,914評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎幼驶,沒想到半個月后艾杏,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,465評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡县遣,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,543評論 3 342
  • 正文 我和宋清朗相戀三年糜颠,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片萧求。...
    茶點故事閱讀 40,675評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡其兴,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出夸政,到底是詐尸還是另有隱情元旬,我是刑警寧澤,帶...
    沈念sama閱讀 36,354評論 5 351
  • 正文 年R本政府宣布守问,位于F島的核電站匀归,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏耗帕。R本人自食惡果不足惜穆端,卻給世界環(huán)境...
    茶點故事閱讀 42,029評論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望仿便。 院中可真熱鬧体啰,春花似錦攒巍、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,514評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至沽翔,卻和暖如春兢孝,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背仅偎。 一陣腳步聲響...
    開封第一講書人閱讀 33,616評論 1 274
  • 我被黑心中介騙來泰國打工跨蟹, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人哨颂。 一個月前我還...
    沈念sama閱讀 49,091評論 3 378
  • 正文 我出身青樓喷市,卻偏偏與公主長得像相种,于是被迫代替她去往敵國和親威恼。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,685評論 2 360

推薦閱讀更多精彩內(nèi)容