heres another way, tho it still does not use chdir, but it does work from the scripts location and all you have to feed it is the absolute path or from script path as arg.
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(); }
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.

EDIT: here is the newest code, which i have not really looked any further into this tbh, but you should be able to add chdir and list dir contents pretty easily. Could maybe even add switches for searching cwd for the string you needed.
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(); }

In reply to Re: Perl script does not work on other directories? by james28909
in thread Perl script does not work on other directories? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.