CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [alex5161] command line option -n , -p with multiple files...: Edit Log



BillKSmith
Veteran

May 30, 2017, 1:59 PM


Views: 4878
Re: [alex5161] command line option -n , -p with multiple files...

It can be done, but it is not easy. You have to study the pseudo code for <> in the IO OPERATORS section of perlop to understand what is going on. I devised the following demo. You have to keep track of the current file yourself.

Code
C:\Users\Bill\forums\guru\alex>type a.txt 
I am from file 'a'.

C:\Users\Bill\forums\guru\alex>type b.txt
I am from file 'b'.

C:\Users\Bill\forums\guru\alex>type c.txt
I am from file 'c'.

C:\Users\Bill\forums\guru\alex>type test.pl
#! /usr/bin/perl -n
use strict;
use warnings;
BEGIN{
print "BEGIN: @ARGV\n";
our $CUR_FILE_NAME = $ARGV[0];
our $NEXT_FILE_NAME = $ARGV[1];
}
our $CUR_FILE_NAME;
our $NEXT_FILE_NAME;


if (!defined $ARGV[0] or $NEXT_FILE_NAME ne $ARGV[0]) {
$CUR_FILE_NAME= $NEXT_FILE_NAME;
$NEXT_FILE_NAME= $ARGV[0];
}
print $CUR_FILE_NAME, ': ', $_, "\n";

C:\Users\Bill\forums\guru\alex>test.pl a.txt b.txt c.txt
BEGIN: a.txt b.txt c.txt
a.txt: I am from file 'a'.

b.txt: I am from file 'b'.

c.txt: I am from file 'c'.


UPDATE: The name of the current input file in available in the special variable $ARGV. This is documented in the section of perlop on the operator <>.
Good Luck,
Bill

(This post was edited by BillKSmith on May 30, 2017, 2:48 PM)


Edit Log:
Post edited by BillKSmith (Veteran) on May 30, 2017, 2:48 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives