Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: Open a directory and recursively process files

by clueless newbie (Chaplain)
on Jan 09, 2013 at 12:58 UTC ( #1012472=note: print w/replies, xml ) Need Help??

in reply to Reaped: Open a directory and recursively process files

Either learn how to use the debugger or Data::Dumper. I've modified your code to dump some values. They are not quite what you expect!

#!/usr/bin/perl -w use Data::Dumper; use strict; my $dir = 'C:\Users\ZB\Desktop\Text Files'; $dir = 'c:'; opendir (DIR, $dir) or die "cannot opendir $dir"; foreach my $file (readdir(DIR)) { warn Data::Dumper->Dump([\$file],[qw(*file)]).' '; &process_file ($file); } sub process_file { warn Data::Dumper->Dump([\@_],[qw(*_)]).' '; my $line = @_; warn Data::Dumper->Dump([\$line],[qw(*line)]).' '; my @array = split(/ /, $line); print "$#array\n"; } closedir (DIR);

And this produces:

Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:>perl $file = \'.'; # This is '.' at line 12. @_ = ( # Your sub get an array containing +'.' '.' ); at line 18. $line = \1; # but $line is 1 (the length of @_) + ... at line 20. # what you really wanted is my ($fi +lename) = @_; # This is the name of the file that needs to be opened/ +read/closed 0 $file = \'..'; at line 12. @_ = ( '..' ); at line 18. $line = \1; at line 20. 0 $file = \'MyOpen(0).pl'; at line 12. @_ = ( 'MyOpen(0).pl' ); at line 18. $line = \1; at line 20. 0 ...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1012472]
[Discipulus]: here spring has sprung, so the warm is available without holidays..
[Discipulus]: Oh I got a (small but better than nothing) salary increase!! beer for all!
[cavac]: Not sure if it's only my connection, but seems rather slow to respond today.
[cavac]: Could some of you just log in for a second and check, for example, the tail daemon logfile?

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2017-02-22 08:57 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (325 votes). Check out past polls.