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

Re: Question about warnings and arrays

by 2teez (Vicar)
on Aug 12, 2014 at 20:52 UTC ( [id://1097181]=note: print w/replies, xml ) Need Help??


in reply to Question about warnings and arrays

Hi james28909

If I could say, why not use a subroutine for repeated works. It's defined a clean and clear path of work.
Since, you are reading through several files, use a subroutine, that read files, and does all you wanted then you can use the result returned. In fact, you don't have to manually close the file, because the file is closed for you once the subroutine is done for each file opened.
Then, you can deal with each part of your work separately.
E.g Let say I want to read line 10, of each file in a particular folder:

use warnings; use strict; my $dirname = $ARGV[0] // '.'; for ( glob("$dirname/*") ) { if ( !-d ) { read_file($_); } } sub read_file { my $filename = shift; open my $fh, '<', $filename or die "can't open file:$! "; while (<$fh>) { print $_ if $. == 10; } }

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://1097181]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-26 02:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found