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

Reaped: Open a directory and recursively process files

by NodeReaper (Curate)
on Jan 09, 2013 at 12:30 UTC ( #1012467=perlquestion: print w/ replies, xml ) Need Help??
NodeReaper has asked for the wisdom of the Perl Monks concerning the following question:

This node was taken out by the NodeReaper on Jan 09, 2013 at 13:15 UTC
Reason: [marto]: Reap: User has several existing threads with the same code/question

You may view the original node and the consideration vote tally.

Comment on Reaped: Open a directory and recursively process files
Replies are listed 'Best First'.
Re: Open a directory and recursively process files
by marto (Bishop) on Jan 09, 2013 at 12:34 UTC

    You (and another user ID posting the same code you've been given) already have active threads discussing this problem. Stop reposting the same question. See Understanding and Using PerlMonks.

Re: Open a directory and recursively process files
by BrowserUk (Pope) on Jan 09, 2013 at 12:38 UTC

    Neither, it is my $line = @_; that is the problem. An array assigned to a scalar gives the number of elements in the array.

    What you should do is either the assignment you weren't sure about (but aren't actually using):my $line = $_[0];

    Or, a list assignment: my( $line ) = @_;

    Or, use shift:my $line = shift;

    You should also drop the & from your subroutine calls. Ie. &process_file ($file); should be process_file( $file );

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Open a directory and recursively process files
by clueless newbie (Hermit) on Jan 09, 2013 at 12:58 UTC

    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: perlquestion [id://1012467]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2016-02-10 08:16 GMT
Find Nodes?
    Voting Booth?

    How many photographs, souvenirs, artworks, trophies or other decorative objects are displayed in your home?

    Results (337 votes), past polls