Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Perl script does not work on other directories?

by 2teez (Vicar)
on Nov 23, 2014 at 10:58 UTC ( [id://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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-19 04:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found