无法使用 perl

问题描述

我写了一个漂亮的小 perl 脚本,对我非常有用。它允许我编译和执行 C 指令,就好像它是解释性语言的指令一样。 这是我用来学习 C 语言的各种 C 编程 IDE。

这是我如何使用它:

crepl -Istdio 'char * str="text"; printf("%s\n",str);'
OUTPUT
text

crepl -Istdio,string 'char * str="text"; int i; for(i=0; i < strlen(str);i++){printf("%c",str[i]);} printf("\n");'
OUTPUT
text

crepl -Istdio 'char * str="text"; int i=0; while(str[i] != '\''\0'\''){printf("%c",str[i]); i++;} printf("\n");'
OUTPUT
text

这是脚本:

#!/usr/bin/perl

# this script requires this line in /etc/fstab :
#tmpfs   /tmp/ram/   tmpfs    defaults,noatime,nosuid,nodev,mode=1777,size=32M 0 0

use strict;
use warnings;
use autodie;

my @include;
$,="\n";
$\="\n";

if (not @ARGV) { exit }
elsif ($ARGV[0] =~ m/^-I/) {
    $ARGV[0] =~ s/^-I//;
    @include = split /,/,$ARGV[0];
    shift;
}

#my $source_code   = "/tmp/ram/c_program.c";    # requires adding a line in /etc/fstab
#my $compiled_code = "/tmp/ram/c_program.o";    # requires adding a line in /etc/fstab
my $source_code   = "./.c_program.c";
my $compiled_code = "./.c_program.o";

open my $FH,">",$source_code;
foreach (@include){
    print $FH "#include <$_.h>";
}
print $FH "int main(int argc,char *argv[]) {";
print $FH $ARGV[0];
print $FH "return 0;";
print $FH "}";
close $FH;

system "gcc",$source_code,"-o",$compiled_code;
system $compiled_code;

我遇到的问题是我没有收到运行时发生的错误消息。 (虽然我确实收到了编译错误消息。)

如果我这样做:

crepl -Istdio 'char * str="text"; char * copy; int i=0; while(str[i] != '\''\0'\''){copy[i]=str[i]; i++;} printf("%s\n",copy);'

我没有输出。但我真的应该得到这个:

分段错误(核心转储)

因为我忘记了 malloc。复制字符串的正确方法是这样的:

crepl -Istdio,string,stdlib 'char * str="text"; char * copy=malloc(strlen(str)); int i=0; while(str[i] != '\''\0'\''){copy[i]=str[i]; i++;} printf("%s\n",copy);'

这很烦人。如果我没有收到诸如 Segmentation Fault 之类的消息,我将很难学习 C。

我试过更换

系统$compiled_code;

系统"$compiled_code 2>&1";

打印`$compiled_code 2>&1`;

但两者都不起作用。

您知道我如何修改脚本以获取这些错误消息吗?

谢谢。

解决方法

您有时在终端中看到的 Segmentation fault (core dumped) 消息不是由您启动的进程产生的,而是由启动该进程的 shell 产生的。

当它启动一个进程时,shell 会使用类似于 man 3 waitpid 的系统调用等待它。 这样的系统调用告诉进程是成功退出(使用 return_exit())还是被信号杀死。 在最后一种情况下,shell 会显示一条特定于导致提前终止 (man 3 strsignal) 的信号的消息。

在您的特定情况下,这不是启动您用 C 编写的进程的 shell,而是 perl 解释器。 您的进程被杀死并不会使 perl 也被杀死,因此您的 shell 不会显示这样的消息。

我不会写 perl,但我确信你可以用相当于 system $compiled_code; 的东西替换 fork()/exec()/waitpid()/strsignal()

使用 this page 的结尾, 我认为您可以在脚本末尾尝试此操作。

system $compiled_code;
my $status = $?;
if ($status == -1) {
    print "failed to execute: $!\n";
}
elsif ($status & 127) {
    printf "child died with signal %d,%s coredump\n",($status & 127),($status & 128) ? 'with' : 'without';
}
else {
    printf "child exited with value %d\n",$status >> 8;
}

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...