D
David Combs
What I want to do traverse files that recursively
"include" or (as in csh .cshrc-files) "source"
other files -- then come back and continue
to process the "sourcing" file.
Files like these:
375 ====> head -5000 test{1,2,3}.source
==> test1.source <==
foo
source test2.source
bar
bletch
==> test2.source <==
this is test2.source, line ONE
source test3.source
this is test2.source, line THREE
this is test2.source, line FOUR
==> test3.source <==
this is test3.source, line ONE.
this is test3.source, line TWO.
376 ====> !! >! foo.out
head -5000 test{1,2,3}.source > ! foo.out
377 ====>
I have tried lots of (probably wrong) things in trying
to have the file-handle being lexically-bound,
so it would reappear in the same state upon
(recursive) return, and the while would keep
reading the lines *after* the "source" line up to
the end of file.
No such luck. You'll see three triples of statements,
two of the triples commented out, and I can't
get any of them to work -- heck, not even to compile!
(Heck, I also have problems getting cperl-mode (emacs)
bounce-parens correctly, eqivalently it complains
when I regionsize the sub, and try cperl-indent-region,
for which cperl-mode gives me:
Scan error: Unbalanced parentheses" 3059, 5237
( giving different locations depending on what triple is uncommented)
, and I have no luck finding anything wrong (unbalanced),
or missing trailing-semicolons, or missing double-quotes,
yada, yada, yada.
FYI: I've been using perl-5.8.6.
(I originally had it at least opening the files in
the right order, but not restoring the prior
filehandle on returning -- but that version
I didn't save away, and it's now transmuted
into this mess:
(Crazy, but I can find no example in any of my many
perl books, nor in any of the (huge amount of) stuff
(threads) I've downloaded from here. The only mention
of "recursive" in the camel-3 is about doing it through
*directories* -- and they (well, he) cheats by using
file::find, instead of doing it "by hand" (to show
how lexical handles would do the right thing on the dfs).)
Thanks much for any help!
(syntax, semantics, as well as the cperl-mode?)
----------------- here it is (ugh!):
#!/me-FIRST-in-PATH-bin/perl-5.8.6/bin/perl5.8.6 -w traverse-via-SOURCE-stmts.pl
use strict;
use diagnostics;
use warnings;
use Carp;
use IO::Handle; # test of 11may05: (recursive opens)
use FileHandle; # ---- from pg 895 in camel 3rd:
# ------------------------------------------------------------------------
# ------------------------------------------------ processOneFile():::
# ------------------------------------------------------------------------
# cperl-indent-region on the entire sub, complains (unfortunately
# the err-msg doesn't make it to *Messages*):
# Scan error: "Unbalanced parentheses", 3002, 5180"
sub processOneFile {
my $fileName = shift;
my $level = shift;
print("\n========================= ENTERING processOneFile(\"$fileName\", $level\n");
# ---- from pg 895 in camel 3rd, (with "my" added):
my $fh = new FileHandle;
die "Cannot open \"$fileName\"!" if (! fh->open( $fileName ));
while (defined(my $line = <$fh>)) { # while:
# my MYFIN; # or is it "my *MYFIN" ... or what?
# open(MYFIN, "<" . $fileName) or die "Cannot open!";
# while (defined(my $line = <MYFIN>)) { # while:
# my $fh = new IO::Handle $fileName, "r";
# die "Cannot open \"$fileName\"!" if (! defined $fh);
# while (defined(my $line = <$fh>)) { # while:
chomp($line);
print "EARLY: $line\n"; # debug.
if ( ( $line =~ /^source / ) &&
( $line =~ /^source\s+([A-Za-z0-9._-]+)/ ) ) { # dive-in:
# CPERL-MODE won't bounce on either rparen ^ ^ -- WHY?
my $diveIntoFName = $1;
print("Now, we'll try to source-in file \"$diveIntoFName\"\n"); # debug.
processOneFile($diveIntoFName, $level + 1); # RECURSE
print("LATE: $line\n"); # debug.
} # dive-in.
} # end-while.
print("----- EXITING processOneFile(\"$fileName\", $level\n\n");
} # end-sub processOneFile.
processOneFile("test1.source", 1);
Thanks!
David
"include" or (as in csh .cshrc-files) "source"
other files -- then come back and continue
to process the "sourcing" file.
Files like these:
375 ====> head -5000 test{1,2,3}.source
==> test1.source <==
foo
source test2.source
bar
bletch
==> test2.source <==
this is test2.source, line ONE
source test3.source
this is test2.source, line THREE
this is test2.source, line FOUR
==> test3.source <==
this is test3.source, line ONE.
this is test3.source, line TWO.
376 ====> !! >! foo.out
head -5000 test{1,2,3}.source > ! foo.out
377 ====>
I have tried lots of (probably wrong) things in trying
to have the file-handle being lexically-bound,
so it would reappear in the same state upon
(recursive) return, and the while would keep
reading the lines *after* the "source" line up to
the end of file.
No such luck. You'll see three triples of statements,
two of the triples commented out, and I can't
get any of them to work -- heck, not even to compile!
(Heck, I also have problems getting cperl-mode (emacs)
bounce-parens correctly, eqivalently it complains
when I regionsize the sub, and try cperl-indent-region,
for which cperl-mode gives me:
Scan error: Unbalanced parentheses" 3059, 5237
( giving different locations depending on what triple is uncommented)
, and I have no luck finding anything wrong (unbalanced),
or missing trailing-semicolons, or missing double-quotes,
yada, yada, yada.
FYI: I've been using perl-5.8.6.
(I originally had it at least opening the files in
the right order, but not restoring the prior
filehandle on returning -- but that version
I didn't save away, and it's now transmuted
into this mess:
(Crazy, but I can find no example in any of my many
perl books, nor in any of the (huge amount of) stuff
(threads) I've downloaded from here. The only mention
of "recursive" in the camel-3 is about doing it through
*directories* -- and they (well, he) cheats by using
file::find, instead of doing it "by hand" (to show
how lexical handles would do the right thing on the dfs).)
Thanks much for any help!
(syntax, semantics, as well as the cperl-mode?)
----------------- here it is (ugh!):
#!/me-FIRST-in-PATH-bin/perl-5.8.6/bin/perl5.8.6 -w traverse-via-SOURCE-stmts.pl
use strict;
use diagnostics;
use warnings;
use Carp;
use IO::Handle; # test of 11may05: (recursive opens)
use FileHandle; # ---- from pg 895 in camel 3rd:
# ------------------------------------------------------------------------
# ------------------------------------------------ processOneFile():::
# ------------------------------------------------------------------------
# cperl-indent-region on the entire sub, complains (unfortunately
# the err-msg doesn't make it to *Messages*):
# Scan error: "Unbalanced parentheses", 3002, 5180"
sub processOneFile {
my $fileName = shift;
my $level = shift;
print("\n========================= ENTERING processOneFile(\"$fileName\", $level\n");
# ---- from pg 895 in camel 3rd, (with "my" added):
my $fh = new FileHandle;
die "Cannot open \"$fileName\"!" if (! fh->open( $fileName ));
while (defined(my $line = <$fh>)) { # while:
# my MYFIN; # or is it "my *MYFIN" ... or what?
# open(MYFIN, "<" . $fileName) or die "Cannot open!";
# while (defined(my $line = <MYFIN>)) { # while:
# my $fh = new IO::Handle $fileName, "r";
# die "Cannot open \"$fileName\"!" if (! defined $fh);
# while (defined(my $line = <$fh>)) { # while:
chomp($line);
print "EARLY: $line\n"; # debug.
if ( ( $line =~ /^source / ) &&
( $line =~ /^source\s+([A-Za-z0-9._-]+)/ ) ) { # dive-in:
# CPERL-MODE won't bounce on either rparen ^ ^ -- WHY?
my $diveIntoFName = $1;
print("Now, we'll try to source-in file \"$diveIntoFName\"\n"); # debug.
processOneFile($diveIntoFName, $level + 1); # RECURSE
print("LATE: $line\n"); # debug.
} # dive-in.
} # end-while.
print("----- EXITING processOneFile(\"$fileName\", $level\n\n");
} # end-sub processOneFile.
processOneFile("test1.source", 1);
Thanks!
David