CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [tbone587] Taking input multiples times and storing into array: Edit Log



Karazam
User

Mar 9, 2011, 11:29 AM


Views: 2177
Re: [tbone587] Taking input multiples times and storing into array

Not sure if you mean to read the whole file into a variable first,
or extract info and put that into a variable.
First case, if $mac holds the MAC address, then


Code
open my $fh, '<', "/path/to/$mac.cfg" or die $!; 
my @cfg = <$fh>;
for my $line ( @cfg ) {
# do stuff with $line
}
close $fh;


In the second case then rather


Code
open my $fh, '<', "/path/to/$mac.cfg" or die $!; 
while (<$fh>) {
# each line is now in $_, do stuff with that
}
close $fh;


The three argument form of open with a lexical filehandle is the preferred way,
although you may often see the old "open(FH, '< filename')".
And always have a die after so you know if things go wrong.
See perlopentut for more details.


(This post was edited by Karazam on Mar 9, 2011, 11:32 AM)


Edit Log:
Post edited by Karazam (User) on Mar 9, 2011, 11:32 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives