TJCooper has asked for the wisdom of the Perl Monks concerning the following question:
I'm trying to deparse the following one-liner (using -MO=Deparse) in-order to construct it as a .pl script:
perl -i.tmp -lane '@ar = split /([ABC])/, $F[5]; $s = 0; $s += $n * ("A" eq $op ? 0 : 1) while ($n, $op) = splice @ar, 0, 2; $w = "g"; $l = length($F[9]); print "$w\t$F[2]\t$F[3]\t$l\t$F[5]\t0" if $F[1] =~ 50; $w = "y"; $l = length($F[9]); $p = $F[3]+$s; print "$w\t$F[2]\t$p\t$l\t$F[5]\t$s" if $F[1] =~ 10; print "Head1\tHead2\tHead3\tHead4\tVar1\tVar2" if $.==1; close ARGV if eof;' *.txt;-MO=Deparse Output:
BEGIN { $^I = ".tmp"; } BEGIN { $/ = "\n"; $\ = "\n"; } LINE: while (defined($_ = <ARGV>)) { chomp $_; our(@F) = split(' ', $_, 0); @ar = split(/([ABC])/, $F[5], 0); $s = 0; $s += $n * ('A' eq $op ? 0 : 1) while ($n, $op) = splice(@ar, 0, 2 +); $w = 'g'; $l = length $F[9]; print "$w\t$F[2]\t$F[3]\t$l\t$F[5]\t0" if $F[1] =~ /50/; $w = 'y'; $l = length $F[9]; $p = $F[3] + $s; print "$w\t$F[2]\t$p\t$l\t$F[5]\t$s" if $F[1] =~ /10/; print "Head1\tHead2\tHead3\tHead4\tVar1\tVar2" if $. == 1; close ARGV if eof; } -e syntax OK
The deparsed output does not appear to include the ability to read in all .txt files within the current directory - without having to specify them on the command line such that they feed into ARGV. I was under the impression I could simply add to line-1:
my @ARGV = glob("*.txt");And each file would be passed to <ARGV> for processing, however when running:
perl script.plWhilst in a current directory containing .txt files, nothing happens. No error is returned, but it does not process anything nor complete. I have also tried to wrap the code in a for-loop using:
my @ARGV = glob("*.txt"); foreach my $ARGV {
And alternatively getting the directory itself:
use cwd my $dir = cwd() foreach my $files (global("$dir/*.txt")) {
But this produces the same issue.
What am I missing? Also, given that I was originally creating backups using -i within the one-liner, how is this now handled within a script? Could this also cause issues?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Loading all .txt files within current directory
by choroba (Cardinal) on Jan 31, 2016 at 12:44 UTC | |
by TJCooper (Beadle) on Jan 31, 2016 at 13:34 UTC | |
|
Re: Loading all .txt files within current directory
by Laurent_R (Canon) on Jan 31, 2016 at 16:12 UTC | |
by Discipulus (Canon) on Feb 01, 2016 at 11:26 UTC | |
|
Re: Loading all .txt files within current directory
by Anonymous Monk on Jan 31, 2016 at 13:33 UTC | |
by TJCooper (Beadle) on Jan 31, 2016 at 13:41 UTC | |
by poj (Abbot) on Jan 31, 2016 at 13:54 UTC | |
by TJCooper (Beadle) on Jan 31, 2016 at 15:34 UTC |