Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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 opendir.pl $file = \'.'; # This is '.' at opendir.pl line 12. @_ = ( # Your sub get an array containing +'.' '.' ); at opendir.pl line 18. $line = \1; # but $line is 1 (the length of @_) + ... at opendir.pl 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 opendir.pl line 12. @_ = ( '..' ); at opendir.pl line 18. $line = \1; at opendir.pl line 20. 0 $file = \'MyOpen(0).pl'; at opendir.pl line 12. @_ = ( 'MyOpen(0).pl' ); at opendir.pl line 18. $line = \1; at opendir.pl line 20. 0 ...

In reply to Re: Open a directory and recursively process files by clueless newbie
in thread Reaped: Open a directory and recursively process files by NodeReaper

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-25 18:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found