定义好了一个可以输出带颜色行号以及行数据的函数print_with_line_num,f()是测试函数。在f()函数中,读取文件并输出读取的每一行数据,但根据参数选项决定是普通输出行还是同时输出带颜色行号的行数据。

这可以当作是偏函数、闭包、作用域的一个用法示例。

脚本内容如下:

#!/usr/bin/perl -w

use strict;
use 5.010; # print string with colored line_num
# arg1: line num
# arg2: string to print
sub print_with_line_num {
eval 'use Term::ANSIColor'; my $line_num = shift;
my $string = shift; my $color = 'bold yellow'; print colored($line_num, $color), ":", "$string";
} # test function
# arg1: filename for read and print
# arg2: a bool to control whether print line num
sub f {
my $filename = shift; # arg: print_line_num or not?(bool)
my $print_line_num = shift; # initialize line_num
my $line_num = 1; # define a printer, according to the bool of print_line_num,
# choose how to print string
my $myprinter;
{
if($print_line_num){
# print line num
# specify the arg1 to line_num
$myprinter = sub { print_with_line_num "$line_num", @_; }
} else {
# don't print line num,
# so make a simple wrapper for builtin print function
$myprinter = sub { print @_; };
}
} open my $fh, "$filename" or die "open failed: $!";
while(<$fh>){
$myprinter->($_);
$line_num++;
}
} if ($ARGV[0] eq "-n"){
f($ARGV[1], 1); # print every line with colored line num
} else {
f($ARGV[0]); # print every line normally
}

下面是测试效果:

普通输出/etc/hosts文件行:

输出带颜色行号的/etc/hosts文件行:

Perl输出带颜色行号或普通输出行的更多相关文章

  1. Java基础(57):Eclipse中环境配置(视图字体颜色行号调试快捷键等等)

    1:Eclipse的基本配置 A:程序的编译和运行的环境配置(一般不改) window -- Preferences -- Java 编译环境:Compiler 默认选中的就是最高版本. 运行环境:I ...

  2. Perl一行式:处理行号和单词数

    perl一行式程序系列文章:Perl一行式 所有行的行号 $ perl -pe '$_ = "$. $_"' file.log $ perl -ne 'print "$. ...

  3. grep[行号&正则匹配字符有颜色]

    事情是这样的,昨天在深入学习grep命令时,看到别人博客用grep正则匹配,不仅行数有颜色,而且匹配到的字符也有颜色.我在CRT也试了下,毛颜色都没有.顿时感觉 so low. 解决 编辑vim~/. ...

  4. 修改vim的颜色主题 及显示行号

    1.打开vim窗口,输入命令:color 或者colorscheme后回车查看当前颜色主题. 2. 输入:colorscheme <主题> 即可设置当前vim的颜色主题. sample: ...

  5. 修改VScode行号区的背景颜色

    vs code是相当不错的一个编辑器,现阶段我用来编写nim程序. vs code非常丧心病狂一点就是行号区和编辑区的背景颜色都是一样, 这样会导致要看行首有没有对齐会非常吃力.这个问题让我纠结了非常 ...

  6. securecrt中vim行号下划线问题及SecureCRT里root没有高亮的设置,修改linux终端命令行颜色

      背景:在用raspberry用SecureCRT下的vim打开文件时出现用set nu时行有下划线,于是找了下解决办法,如下:vim行号下划线问题在vim中发现开启显示行号(set number) ...

  7. mac/linux中vim永久显示行号、开启语法高亮

    步骤1: cp /usr/share/vim/vimrc ~/.vimrc 先复制一份vim配置模板到个人目录下 注:redhat 改成 cp /etc/vimrc ~/.vimrc 步骤2: vi ...

  8. php 扫描url死链接 \033[31m ANSI Linux终端输出带颜色

    * 从Packagist上搜索需要的包 https://packagist.org/ * 通过composer下载依赖包 composer require guzzlehttp/guzzlecompo ...

  9. vim显示行号、语法高亮、自动缩进的设置

    转载自:http://blog.csdn.net/chuanj1985/article/details/6873830   在UBUNTU中vim的配置文件存放在/etc/vim目录中,配置文件名为v ...

随机推荐

  1. 关于Django报错django.core.exceptions.ImproperlyConfigured: Requested setting DEBUG, but settings are not configure

    报错代码:django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but se ...

  2. ASP.NET Core 快速入门(环境篇)

    [申明]:本人.NET Core小白.Linux小白.MySql小白.nginx小白.而今天要说是让你精通Linux ... 的开机与关机.nginx安装与部署.Core的Hello World .. ...

  3. 微信小程序初体验,入门练手项目--通讯录,部署上线(二)

    接上一篇<微信小程序初体验,入门练手项目--通讯录,后台是阿里云服务器>:https://www.cnblogs.com/chengxs/p/9898670.html 开发微信小程序最尴尬 ...

  4. javaScript设计模式之面向对象编程(object-oriented programming,OOP)(二)

    接上一篇 面向对象编程的理解? 答:面向对象编程,就是将你的需求抽象成一个对象,然后针对这个对象分析其特征(属性)与动作(方法).这个对象我们称之为类.面向对象编程思想其中一个特点就是封装,就是把你需 ...

  5. [Leetcode]450. Delete Node in a BST

    Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...

  6. .NET Core Session的简单使用

    前言 在之前的.NET 里,我们可以很容易的使用Session读取值.那今天我们来看看 如何在.NET Core中读取Session值呢? Session 使用Session之前,我们需要到Start ...

  7. 解决WebMagic抓HTTPS时出现SSLException

    访问我的博客 前言 在今年二月份在项目中引入了 WebMagic 技术,用来抓取合作方的书籍,详见之前文章:WebMagic之爬虫监控,这两天新接入了一个合作商,对方接口采取的是 HTTPS 协议,而 ...

  8. 使用Atlas进行元数据管理之Glossary(术语)

    背景:笔者和团队的小伙伴近期在进行数据治理/元数据管理方向的探索, 在接下来的系列文章中, 会陆续与读者们进行分享在此过程中踩过的坑和收获. 元数据管理系列文章: [0] - 使用Atlas进行元数据 ...

  9. [JavaScript] 函数节流(throttle)和函数防抖(debounce)

    js 的函数节流(throttle)和函数防抖(debounce)概述 函数防抖(debounce) 一个事件频繁触发,但是我们不想让他触发的这么频繁,于是我们就设置一个定时器让这个事件在 xxx 秒 ...

  10. MySQL事务及ACID特性

    一.事物 1.定义:事务是访问和更新数据库的程序执行单元,事务中包含一条或者多条SQL语句,这些语句要么全部执行成功,要么都不执行. 在MySQL中,事务支持是在引擎层实现的,MySQL是一个支持多引 ...