Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

executing perl command from a perl file

by kulua (Initiate)
on May 24, 2021 at 17:14 UTC ( [id://11132976]=perlquestion: print w/replies, xml ) Need Help??

kulua has asked for the wisdom of the Perl Monks concerning the following question:

How to execute

perl -F, -lanE 'm/module/?say:say qq|.$F[0]($F[0])$F[1]|' < trial1.txt

Is it

eval 'exec perl -F, -lanE 'm/module/?say:say qq|.$F[0]($F[0])$F1|' < trial1.txt'

command from a perl file

Replies are listed 'Best First'.
Re: executing perl command from a perl file
by haukex (Archbishop) on May 24, 2021 at 18:12 UTC
    How to execute perl -F, -lanE 'm/module/?say:say qq|.$F[0]($F[0])$F[1]|' < trial1.txt

    In your thread To repeat each word in a line, where you were given this code, kcott and AnomalousMonk both gave you some example code that included while loops. The code 'm/module/?say:say qq|.$F[0]($F[0])$F[1]|' is what would go in the body of the loop, plus the code provided by the -F, -lan switches, which you can see by adding -MO=Deparse:

    $ perl -MO=Deparse -F, -lanE 'm/module/?say:say qq|.$F[0]($F[0])$F[1]| +' BEGIN { $/ = "\n"; $\ = "\n"; } use feature 'current_sub', 'bitwise', 'evalbytes', 'fc', 'postderef_qq +', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval'; LINE: while (defined($_ = readline ARGV)) { chomp $_; our @F = split(/,/u, $_, 0); /module/u ? say($_) : say(".$F[0]($F[0])$F[1]"); }

    This can be cleaned up to:

    use warnings; use strict; use feature 'say'; while ( my $line = <> ) { chomp $line; my @F = split /,/, $line; if ( $line =~ /module/ ) { say $line; } else { say ".$F[0]($F[0])$F[1]"; } }

    Though you probably want to replace the <> with a filehandle from an opened file, as has been shown to you.

Re: executing perl command from a perl file
by BillKSmith (Monsignor) on May 24, 2021 at 19:58 UTC
    I am sorry if I am wrong, but it sounds like your only interest in Perl is a solution to yesterday's problem. If so, you probably have come to the wrong place. We are here to help you solve it yourself. You received two good answers. The first was the terse one-line program you have repeated here along with a link to the documentation needed to understand it. Once you understand it, you should be able to adapt it to your needs without executing perl from within perl. (Did you notice that it omits the final ');'? ) The other answer is much longer, but easier to understand. You may even be able to cut and paste the parts that you need.
    Bill
Re: executing perl command from a perl file
by The Perlman (Scribe) on May 24, 2021 at 17:37 UTC
    See qx, exec doesn't return.
    - Ron
      See qx, exec doesn't return.

      Sorry, but I don't think this is good advice. Even if it were necessary to run an external perl, which it practically never is except maybe in a test suite, qx has quite a few caveats.

        True, but I doubt the OP will read a longer answer. ;)
        - Ron
        PS: but thanks for the interesting link :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11132976]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2024-03-28 10:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found