将php.inc文件导入PERL程序

将php.inc文件导入PERL程序,php,linux,perl,Php,Linux,Perl,我正在尝试为perl和php使用1个include文件 有没有在perl中导入myphp.inc文件的好方法 $ cat myphp.inc <?php $some_var="hello world"; ?> 如果我删除

我正在尝试为perl和php使用1个include文件

有没有在perl中导入myphp.inc文件的好方法

$ cat myphp.inc
<?php
    $some_var="hello world";
?>
如果我删除<?php然后test.php将只打印出myphp.inc文件的内容。。。如果我将它们留在其中,那么我的perl程序会抱怨:

Unterminated <> operator
我已经看过perl模块:PHP::Include,但是如果可能的话,我希望远离外部模块


任何人都有这样做的想法???

不要试图编写PHP和Perl代码,它们是不同的语言,即使它们有一些共同的祖先。如果要在两者之间共享数据,请使用结构化数据格式。这是一种流行的口味。PHP有模块,Perl有模块

如果可能的话,我想远离外部模块


代码重用是一种美德……尽管没有什么能阻止您从头开始重新实现模块。

不要尝试编写PHP和Perl代码,它们是不同的语言,即使它们有一些共同的祖先。如果要在两者之间共享数据,请使用结构化数据格式。这是一种流行的口味。PHP有模块,Perl有模块

如果可能的话,我想远离外部模块


代码重用是一种美德……尽管没有什么能阻止您从头开始重新实现模块。

理想情况下,您可以将共享/配置数据存储为PHP和Perl中易于读取的格式。XML、JSON或simbabque建议使用的带有键值对的简单文本文件(如.ini文件)将非常有用

如果您决定用Perl读取PHP文件,但不想使用诸如PHP::Include之类的模块,则只需编写如下内容:

use IO::File;

sub require_php {
        my $source_filename = $_[0];
        my $dest_filename = 'temp.inc.pl';
        open my $source, $source_filename or die "Could not open $source_filename: $!";
        open my $destination, '>>'.$dest_filename or die "Cound not open file for writing";
        while(my $line = <$source>) {
                if(index($line,'<?php')==-1 && index($line,'?>')==-1) {
                        print $destination $line
                }
        }
        close $destination;
        close $source;
        require $dest_filename;
        unlink $dest_filename;
}

our $some_var = '';
require_php('myphp.inc');

这将以$some_var结束,其值为hello world。

理想情况下,您将以易于在PHP和Perl中读取的格式存储共享/配置数据。XML、JSON或simbabque建议使用的带有键值对的简单文本文件(如.ini文件)将非常有用

如果您决定用Perl读取PHP文件,但不想使用诸如PHP::Include之类的模块,则只需编写如下内容:

use IO::File;

sub require_php {
        my $source_filename = $_[0];
        my $dest_filename = 'temp.inc.pl';
        open my $source, $source_filename or die "Could not open $source_filename: $!";
        open my $destination, '>>'.$dest_filename or die "Cound not open file for writing";
        while(my $line = <$source>) {
                if(index($line,'<?php')==-1 && index($line,'?>')==-1) {
                        print $destination $line
                }
        }
        close $destination;
        close $source;
        require $dest_filename;
        unlink $dest_filename;
}

our $some_var = '';
require_php('myphp.inc');

这将以$some_var结束,其值为hello world。

我只是想为这两个变量设置一些全局变量,这样我就不必编辑我的perl.inc和php.inc文件。嗯,我不确定我是否买这个。。。因此,当我有一些全局变量时,比如$MAX_ROWS和$CUSTOMER_,我的php和perl程序都使用总共30个变量,我应该用其他格式对它们进行编码,并用ie JSON?读取它们。。。好像overkill@Douglas,将$MAX_行和好友视为运行时配置参数。以对您的程序和平台有意义的格式存储它们—配置文件JSON?、注册表、设置环境变量的包装器脚本、web服务等等。即使是只有key:value或key=value行的.ini类文件也比这更有意义。它不必是JSON,它很方便,因为它很容易解析。通过这种方式,用户也可以更容易地编辑这些配置参数。我只是想为这两者设置一些全局变量,这样我就不必编辑perl.inc和php.inc文件。嗯,我不确定我是否买这个。。。因此,当我有一些全局变量时,比如$MAX_ROWS和$CUSTOMER_,我的php和perl程序都使用总共30个变量,我应该用其他格式对它们进行编码,并用ie JSON?读取它们。。。好像overkill@Douglas,将$MAX_行和好友视为运行时配置参数。以对您的程序和平台有意义的格式存储它们—配置文件JSON?、注册表、设置环境变量的包装器脚本、web服务等等。即使是只有key:value或key=value行的.ini类文件也比这更有意义。它不必是JSON,它很方便,因为它很容易解析。通过这种方式,用户也更容易编辑这些配置参数。