ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
# Open cfg file
open( CONFIG_FILE, "<$cfg_file" ) or
die "Can't open cfg file: $cfg_file: $!\n";
# Process cfg file records
while ( defined ( $cfg_rec = <CONFIG_FILE> ) )
{
# Remove unwanted chars
chomp $cfg_rec; # newline
# your code here
}
# Close cfg file
close (CONFIG_FILE) or
die "Can't close cfg file: $cfg_file: $!\n";
Because this is a common operation and that's a lot to type, Perl gives it a shorthand notation. This shorthand reads lines into $_ instead of $line. Many other string operations use $_ as a default value to operate on, so this is more useful than it may appear at first:
while (<DATAFILE>) {
chomp;
print length, "\n"; # output size of line
}
True, but I always use named vars, not $_. I often write substantial sized Perl progs, so I like to have all my vars explicit.
Possibly also due to having done about 9 yrs of C first...
True, but I always use named vars, not $_. I often write substantial sized Perl progs, so I like to have all my vars explicit.
Possibly also due to having done about 9 yrs of C first...
@Ifeatu: The modern norm is to use a three-argument form of open and lexically scoped filehandles (variables declared with my rather than barewords like FILE). You should also always check the return status of open (and close for that matter).
A simple example might look like this:
Code:
#!/usr/bin/env perl
use strict;
use warnings;
my $file = 'example.txt';
open my $file_handle, '<', $file
or die "Can't open [$file] for reading: $!";
while (my $line = <$file_handle>) {
print $line;
}
close $file_handle or die "Problem closing [$file]: $!";
You can use autodie to get rid of some of the boilerplate around open and close.
#!/usr/bin/env bash
# @(#) s1 Demonstrate running perl script.
echo
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) perl
set -o nounset
echo
echo " perl code:"
cat p1
echo
echo " Data file data1:"
cat data1
echo
echo " Results:"
./p1
exit 0
producing:
Code:
% ./s1
Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution : Debian GNU/Linux 5.0
GNU bash 3.2.39
perl 5.10.0
perl code:
#!/usr/bin/perl
# @(#) p1 Demonstrate read, <>, without defined.
use warnings;
use strict;
use feature qw(switch say);
use 5.010;
my ($debug);
$debug = 0;
$debug = 1;
while ( my $line = <DATA> ) {
print $line;
}
print "\n";
open( my $data, "<", "data1" ) or die " Cannot open file data1.\n";
while ( my $line = <$data> ) {
print $line;
}
exit(0);
__DATA__
0
one
two
last
Data file data1:
0
first
second
final
Results:
0
one
two
last
0
first
second
final
Data includes empty line and "0".
At one time I had always used function defined in such situations. I don't know when the requirement changed (or apparently changed) ... cheers, makyo
Well, yes and no. Your DATA section includes lines consisting of "\n" and "0\n". Both of those, of course, come out true in a boolean context. I suppose that the fear is the edge case of a file with 0 and no newline character (which is unusual but can happen). Still, I can't say that it keeps me up nights.
... I suppose that the fear is the edge case of a file with 0 and no newline character (which is unusual but can happen). ...
Thanks; I agree about newline alone and "0" followed by newline. However, the Programming perl, 3rd, page 81, says:
Both of these while loops still implicitly test for whether the result of the assignment is defined ...
Adding then a "0" with no newline at the end of datafile data2, and running slightly modified versions of the previously posted codes now called s2 and p2:
Code:
#!/usr/bin/env bash
# @(#) s2 Demonstrate running perl script.
echo
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) perl
set -o nounset
echo
echo " perl code:"
cat p2
echo
echo " Data file data2:"
cat data2
echo
echo " Results:"
./p2
echo "That was the final line."
exit 0
produces:
Code:
% ./s2
Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution : Debian GNU/Linux 5.0
GNU bash 3.2.39
perl 5.10.0
perl code:
#!/usr/bin/perl
# @(#) p2 Demonstrate read, <>, without defined.
use warnings;
use strict;
use feature qw(switch say);
use 5.010;
my ($debug);
$debug = 0;
$debug = 1;
while ( my $line = <DATA> ) {
print $line;
}
print "\n";
open( my $data, "<", "data2" ) or die " Cannot open file data1.\n";
while ( my $line = <$data> ) {
print $line;
}
exit(0);
__DATA__
0
one
two
last
Data file data2:
0
first
second
final
0
Results:
0
one
two
last
0
first
second
final
0That was the final line.
So even the odd case seems to be handled without using the function defined explicitly ... cheers, makyo
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.