Questions in category: Perl (Perl)
软件 >> Perl

1. 如何去除文本文件中每一行前三个字符?

Posted by haifeng on 2021-10-27 14:28:26 last update 2021-10-27 14:28:26 | Answers (0) | 收藏


 

经常会碰到一些源文件, 每一行带有行号. 比如下面的代码, 来自 算术表达式解析(第三版)词法分析版 - mjwk - 博客园 (cnblogs.com)

如何去掉行首的行号?

 

 

//用于词法定界的头文件 token.h 1 #ifndef _TOKEN_H_VERSION_20140930
 2 #define _TOKEN_H_VERSION_20140930
 3 
 4 enum TokenKind{
 5     BAD_TOKEN,
 6     NUMBER_TOKEN,
 7     ADD_OPERATOR_TOKEN,
 8     SUB_OPERATOR_TOKEN,
 9     MUL_OPERATOR_TOKEN,
10     DIV_OPERATOR_TOKEN,
11     LEFT_PAREN_TOKEN,
12     RIGHT_PAREN_TOKEN,
13     END_OF_LINE_TOKEN,
14 };
15 
16 #define MAX_TOKEN_SIZE (100)
17 
18 struct Token{
19     TokenKind kind;
20     double value;
21     char str[MAX_TOKEN_SIZE];
22 };
23 
24 void set_line(char* line);
25 void get_token(Token* token);
26 
27 #endif

 

2. 若已经按照 Perl, 如何添加模块?

Posted by haifeng on 2021-10-25 10:01:21 last update 2021-10-25 10:12:05 | Answers (1) | 收藏


比如现在需要在 Perl 中使用 ImageMagick, 在 .pl 文件中通常要写

use Image::Magick;

也就是启用 Image::Magick 模块. 如果 Perl 是默认安装的, 则不会预先有此模块. 在执行 .pl 文件时, 会提示下面的信息

 

Can't locate Image/Magick.pm in @INC (you may need to install the Image::Magick module) (@INC contains: C:/Strawberry/perl/site/lib C:/Strawberry/perl/vendor/lib C:/Strawberry/perl/lib) at test.pl line 6.
BEGIN failed--compilation aborted at test.pl line 6.


如果安装的是 strawberry Perl, 则已经安装了 cpanm. 在 Windows 下可以通过 where cpanm 查看其是否能直接调用.

然后通过 

cpanm Module::Name

直接安装需要的模块. 模块可以在 CPAN 网上查找.

例如: 在命令符窗口下键入 cpanm Image::Magick  就可以安装 Image::Magick 模块.

 

cpanm Image::Magick
--> Working on Image::Magick
Fetching http://www.cpan.org/authors/id/J/JC/JCRISTY/Image-Magick-7.0.11-3.tar.gz ... OK
Configuring Image-Magick-7.0.11-3 ... OK
Building and testing Image-Magick-v7.0.11 ... FAIL
! Installing Image::Magick failed. See C:\Users\haife\.cpanm\work\1635127678.42820\build.log for details. Retry with --force to force install it.

 

 


键入 perl -MCPAN -e shell 可进入 CPAN 的 shell 模式

 

perl -MCPAN -e shell
Unable to get Terminal Size. The Win32 GetConsoleScreenBufferInfo call didn't work. The COLUMNS and LINES environment variables didn't work. at C:\Strawberry\perl\vendor\lib/Term/ReadLine/readline.pm line 410.

cpan shell -- CPAN exploration and modules installation (v2.28)
Enter 'h' for help.

cpan>