Set a shell array from an array in Perl script

后端 未结 3 1839
旧巷少年郎
旧巷少年郎 2021-01-29 07:23

I have the following Perl script:

 sub {
    my $sequence=\"SEQUENCE1\";
    my $sequence2=\"SEQUENCE2\";
    my @Array = ($sequence, $sequence2);
    return \\@         


        
3条回答
  •  無奈伤痛
    2021-01-29 07:49

    Did you test your Perl script? In order to have that Perl script give you something to put into your shell script, you to make sure your Perl script works:

    $ test.pl
    

    No output at all.

    First issue, you put the whole Perl script in sub. Subroutines in Perl don't execute unless you call them. You can't even do that since your subroutine doesn't even have a name. Let's get rid of the subroutine:

    my $sequence="SEQUENCE1";
    my $sequence2="SEQUENCE2";
    my @Array = ($sequence, $sequence2);
    print  \@Array . "\n";
    

    Okay, now let's try the program:

    $ test.pl   
    ARRAY(0x7f8bab8303e0)
    

    You're printing out an array reference with that \ in the front of @Array. Let's print out the array itself:

    my $sequence="SEQUENCE1";
    my $sequence2="SEQUENCE2";
    my @Array = ($sequence, $sequence2);
    print  @Array, "\n";
    

    That will now print @Array:

    $ test.pl
    SEQUENCE1SEQUENCE2
    

    Not quite. There's no spaces between each element. Let's set $, which is the output field separator to be a single space:

    my $sequence="SEQUENCE1";
    my $sequence2="SEQUENCE2";
    my @Array = ($sequence, $sequence2);
    $,=' ';
    print @Array, "\n";
    

    Now:

    $ test.pl
    SEQUENCE1 SEQUENCE2
    

    Now, we have a working Perl program that outputs what we need to put into your shell array:

    seq=($(test.pl))
    echo ${seq[*]}
    SEQUENCE1 SEQUENCE2
    

    When you have an issue, you need to break it down into pieces. Your first issue is that your Perl script wasn't working. Once that was fixed, you could now use it to initialize your array in your Bash shell.

提交回复
热议问题