Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: Perl script does not work on other directories?

by 2teez (Vicar)
on Nov 23, 2014 at 10:58 UTC ( #1108152=note: print w/replies, xml ) Need Help??

in reply to Perl script does not work on other directories?

I think you want to use chdir to change into the directory you want.
Using opendir, you can do like so:

use warnings; use strict; print "Enter a directory:"; chomp( my $dir = <STDIN> ); print "String looked for: "; chomp( my $str = <STDIN> ); chdir $dir or die $!; opendir DH, $dir or die $!; while ( readdir(DH) ) { next if $_ eq '.' or $_ eq '..'; unless (-d) { open my $fh, '<', $_ or die $!; while (<$fh>) { print $_ if /\Q$str/; last; } } } closedir DH or die $!;
I also think, it would be great if you can open the files one after the other immediately and check for desired string.

If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1108152]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2021-04-16 03:41 GMT
Find Nodes?
    Voting Booth?

    No recent polls found