HI,
I'm new to perl and need simple regex for reading a file using my perl script.
The text file reads as -
filename=/pot/uio/current/myremificates.txt
certificates=/pot/uio/current/userdir/conf/user/gamma/settings/security/
key_certificates=/pot/uiopoyu/current/userdir/conf/user/gamma/settings/
I need the left and right part of the text line separated by '=' to be returned to two variables within a for loop as below.
open (MYFILE, 'property.txt');
@array = <MYFILE>;
foreach (@array) {
chomp $_;
//
// Here, i need the regex.
//
}
($var1, $var2) = split(/=/, $_)
1 Like
open(my $myfile, '<', 'property.txt')
or die "Could not open property.txt: $!\n";
while(<$myfile>) {
chomp;
if(my ($var1, $var2) = /(.*?)=(.*)/) {
### do something with $var1 and $var2 ###
}
}
Some advice:
- Bareword filehandles (such as
MYFILE
) are not recommended.
- Always test for exceptions.
- The 3-argument form of
open
, which explicitly specifies the mode of opening the file, is recommended.
- Never ever read from a filehandle (or filehandle reference) in list context. It loads the full file in memory, which might blow up your program in case of large files.
- The default operand for chomp is
$_
.
1 Like
Thanks elixir_sinari once again Will remember your advice.