in reply to Perl script does not work on other directories?
to me its more manageable if it is broken up in subroutines. that way you can have one function to get the input, then the other to actually collect results.use strict; use warnings; use diagnostics; use File::Slurp; #use Cwd; my @files; get_args(); sub get_args { my $dir; my $string; print "enter path\n"; chomp( $dir = <STDIN> ); print "enter match string\n"; chomp( $string = <STDIN> ); print "\n"; @files = read_dir($dir); print $_ for @files ; traverse( $dir, $string ); } sub traverse { foreach my $element (@files) { open $file, "<", $dir.'/'.$element; while (<$file>) { if ( $_ =~ m/$string/i ) { print "found $string in $element\n"; } } close $file; } get_args(); }
use strict; use warnings; use diagnostics; use File::Slurp; #use Cwd; my @files; get_args(); sub get_args { print "enter path: "; chomp( my $dir = <STDIN> ); print "enter match string: "; chomp( my $string = <STDIN> ); @files = read_dir($dir); print "\n", $_ for @files, "\n" ; traverse( $dir, $string ); } sub traverse { my ( $dir, $string ) = @_; for my $element (@files) { open (my $file, '<', "$dir/$element") || next; while (<$file>) { if ( $_ =~ m/$string/i ) { print "found $string in $element\n"; } } close $file; } print "\n"; get_args(); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Perl script does not work on other directories?
by Anonymous Monk on Nov 24, 2014 at 10:38 UTC | |
by james28909 (Deacon) on Nov 24, 2014 at 17:38 UTC | |
by Anonymous Monk on Nov 24, 2014 at 23:27 UTC | |
by james28909 (Deacon) on Nov 25, 2014 at 17:31 UTC | |
|
Re^2: Perl script does not work on other directories?
by james28909 (Deacon) on Nov 26, 2014 at 03:30 UTC | |
by james28909 (Deacon) on Nov 26, 2014 at 17:32 UTC | |
by SuicideJunkie (Vicar) on Nov 26, 2014 at 18:45 UTC | |
by james28909 (Deacon) on Nov 26, 2014 at 20:01 UTC |