1

I'm a beginner at Perl. I'm not understanding the following code snippet.

  my @savedFiles;
  my $cmd = 'su - dbinst -c "ls <some_path>$tag* | sort -r"';
  @savedFiles =  `$cmd`;

What's going on when the array is being assigned a command-line statement? I tried it in my perl6 shell and it gave me the following error.

===SORRY!=== Error while compiling:
Missing required term after infix
------> @savedFiles[0] =⏏ `$cmd`
    expecting any of:
        prefix
        term
Håkon Hægland
  • 39,012
  • 21
  • 81
  • 174
rrtong
  • 11
  • 2
  • 6
    Perl 6 is a very different language from Perl 5. Which language are you using? – Jim Garrison Feb 15 '21 at 23:31
  • 2
    First thing to do is put `use warnings;` and `use strict;` at the top of your Perl code. Next is to `print $cmd` so you can see example what it evaluates out to. But beyond that we can't tell because it's not the real code that you're running, unless you actually have `` in your code. – Andy Lester Feb 15 '21 at 23:32
  • Thanks for bringing up your point @JimGarrison. I was told to translate an existing perl script. My compiler is perl6, which may likely not be the same perl version. – rrtong Feb 16 '21 at 00:00
  • If it's an existing Perl script, you should almost surely be using the latest Perl 5. There is very little Perl 6 code out there (by comparison). Install Perl5 and try there. You can get an interactive debug shell in Perl 5 with `perl -d command.pl`, or a debug shell with no loaded file with `perl -de0`. There's also a shell called IDLE (namesake Eric Idle, of course) but I haven't used that. – Jim Garrison Feb 16 '21 at 00:01
  • 1
    These are NOT different versions, perl5 and perl6, as it seems; the new perl6 is a different language. The name "perl6" is in fact so misleading that they "officially" renamed it to "[raku](https://en.wikipedia.org/wiki/Raku_(programming_language))". What you show is perl5 code, not perl6. The latest version that you can have in perl5 is v5.32.1 – zdim Feb 16 '21 at 02:14
  • 2
    In Perl "[context](https://perldoc.perl.org/perldata#Context)" is critical -- great many things behave differently whether they are in a context in which they must return a scalar, or an array. (Like assigning to a scalar or an array variable.) As for "backticks" which execute that `$cmd` (ithe operator for that is [qx](https://perldoc.perl.org/perlop#qx/STRING/)), when you assign their return to a scalar then they return all of the executed command's output as one (perhaps multiline) string; when you assign to an array, they return the command's output as lines, one per array element. – zdim Feb 16 '21 at 02:24
  • 1
    (in the last sentence above I meant to say that when assigned to an array the backticks return a _list_, each element being one line of output of that command, and so that's what the array elements become) – zdim Feb 16 '21 at 07:44
  • 1
    @rrtong Sorry about the confusion. Questions receive attention based on their tags, and the only programming language tag on your question was `perl` until a few hours ago. I think I am the first raku programmer to comment on your question. It sounds like you wish to translate a perl script to raku. If so, please post *another* question that is an *exact copy* of this one, except for the following two changes. **1** Change the word "perl" in your question *title* to "raku". **2** Add the *tag* `raku` to your new question instead of `perl`. You will then hopefully get the answer you seek. – raiph Feb 16 '21 at 21:52
  • 1
    @raiph Sorry for the long response, but no I was translating from Perl to Python. Having little knowledge of perl or raku, I hadn't known there was a difference until now. Thanks everyone. – rrtong Mar 05 '21 at 04:04
  • @rrtong. It's somewhat like C and C#, or Java and JavaScript. JavaScript was named as it was due to a 1995 agreement between Netscape and Sun. One consequence was that for the first decade or two of their existence many folk thought they were essentially the same. But in fact there's a difference, as you now know. Good luck with the translation. :) – raiph Mar 05 '21 at 12:05

0 Answers0