在perl中返回变量和字符串的组合

时间:2016-03-22 00:24:34

标签: git perl curl

我有一个从配置文件中读取的模块。它将配置文件中的值提取并存储到$ path,$ file,$ host中。当我将这些值传递给一个新的子程序并用字符串连接并尝试返回它时,它会失败。给我一个错误:

模块:

package Module;

use strict;
use Carp;

# Constructor and initialisation 
sub new {                               #class method
    my $class = shift;              #shift without arguments is shift @_ , takes 1st element of argument array
    my $self = {};          #created a Hash reference with @_ helping to store values in a hash
    bless ($self, $class);          #turning self into an object by telling which class it belongs to without hardcode name in
    $self->{_created} = 1; #syntax for accessing the contemts of a hash: refrence $object_name->{property_name}.
    return $self;
}

 #reading from config file
 sub read {
    my ($self, $file) = @_;
    my ($line, $section);
    open(HFILE, "$file") || die "Could not open file '$file' $!";
    $self->{_filename} = $file;     # Store a special property containing the name of the file

    while (chomp (my $line = <HFILE>))
    {
            if ($line =~ /^\[(.*)\]/)
            {
                    $section = $1;
            }
            elsif ($line =~ /^([^=]+)=(.*)/)
            {
                    my ($config_name, $config_val) = ($1, $2);
                    if ($section)
                            {
                                    $self->{"$section.$config_name"} = $config_val;
                            } else {
                                    $self->{$config_name} = $config_val;
                                    }
            }
    }

    close HFILE;
    return 1;
    }



#fetching values needed
sub fetch {
    my ($self, $key) = @_;
    return $self->{$key};
}

sub _setup{
    my ($self, @location) = @_;
    my $command = ''.$location[1].' --eg-config '.$location[0].' -H "Host:'.$location[2].'" -ik https://'.$location[2].'';
    return $self->$command;
}

脚本:

#!/usr/bin/perl

use Module;
use strict;

my $value = Module->new();

$value->read("/Users/hhansraj/git/edgegrid-curl/api.txt") or die "Couldn't read config file: $!";
my $path=$value->fetch('location.path');
my $file=$value->fetch('location.file');
my $host=$value->fetch('credentials.host');
$value->_setup($path,$file,$host);

2 个答案:

答案 0 :(得分:2)

由于您正在阅读INI文件,因此我强烈建议您使用以下配置模块:Config::ZOMGConfig::INI

后者应该很简单:

my $config = Config::INI::Reader->read_file('config.ini');
print $config->{location}->{file};

使用像Moo这样适当的OO,您的代码将是:

<强> MyClass.pm:

package MyClass;

use Config::INI::Reader;

use Moo;

has 'config_file' => (is => 'ro',   required => 1);
has 'config'      => (is => 'lazy', default => sub { Config::INI::Reader->read_file(shift->config_file); });

sub setup {
    my ($self) = @_;

    my $path = $self->config->{location}->{path};
    my $file = $self->config->{location}->{file};
    my $host = $self->config->{credentials}->{host};

    return sprintf "%s --eg-config '%s' -H 'Host: %s' -ik https://%s", ($file, $path, $host, $host); 
}

<强>脚本:

use MyClass;

my $mc = MyClass->new( config_file => 'config.ini');
$mc->setup();

无论如何,你的文件的实际问题是你试图在$command的内容之后以$ self命名一个方法。也许您只想在_setup方法中返回$ command变量?

答案 1 :(得分:1)

我怀疑你想要 _setup 方法来实际执行命令。你最好的选择是system()。要使用&#34; system&#34;,您需要检查返回值是否为0.来自之前链接的文档:

Since "SIGINT" and "SIGQUIT" are ignored during the execution
of "system", if you expect your program to terminate on receipt
of these signals you will need to arrange to do so yourself
based on the return value.

   @args = ("command", "arg1", "arg2");
   system(@args) == 0
       or die "system @args failed: $?"

在您这样做之前,我强烈建议您首先通过将其打印到标准输出来验证最终命令字符串是否完全符合您的要求。由于这是配置/设置模块的一部分,您可以添加另一个名为&#34; testing&#34;的配置选项。或类似的东西。然后,当调用 _setup()时,它可以打印命令或执行它,具体取决于&#34;测试&#34;的值。

如果返回值非零,则出现问题,您可能还会 die()。但是,一旦你解决了bug并将其推迟,命令失败通常是由模块用户提供的错误配置值引起的 - 所以,你应该使用croak() from the Carp module(我注意到你&# 39;已经使用该模块)。这将从调用者的角度突出失败,而不是从模块的角度来看。

把这个togeather给我们类似的东西:

    ... as above ...
    bless ($self, $class);       #turning self into an object by telling which ...
    $self->{_created} = 1;       #syntax for accessing the contemts of a hash: ...
    $self->{_testing} = 0        # Initialize new config option
    return $self;
}
... etc ...

sub _setup{
    my ($self, @location) = @_;
    my $command = sprintf("%s --eg-config '%s' -H 'Host: %s' -ik https://%s",
                          @location[1,0,2,2]);
    if $self->_testing {
        print "Would have executed: $command\n"
    }
    else {
        system($command) == 0 or croak "system $command failed: $?"
    }
}