|
Prev: Please help me pass an array from VBA to Perl and populate it.Newbie at wits' end!
Next: Problems at writing multiple files
From: mark on 4 Oct 2006 19:26 Hello, I have following program in linux: open(DATA, "/etc/hostname"); $hostname = <DATA>; print $hostname if ($hostname eq "debian01") { print "OK"; } close(DATA); Now, when I execute it I just got: debian01 so it seems that $hostdata = "debian01" but there is no 'OK' string :/. What is wrong?!? How can I make it working?!? Regards, mark
From: Mirco Wahab on 4 Oct 2006 19:50 Thus spoke mark (on 2006-10-05 01:26): > I have following program in linux: > ... > Now, when I execute it I just got: > debian01 > so it seems that $hostdata = "debian01" but there is no 'OK' string :/. > What is wrong?!? How can I make it working?!? > open(DATA, "/etc/hostname"); > $hostname = <DATA>; > print $hostname > if ($hostname eq "debian01") { > print "OK"; > } > close(DATA); there are some problems, most will be found by Perl if you start your program with: use warnings; use strict; ... To read the content into a scalar, you could 'slurp' the file, but for beginners, its better to read into an array and take the first element as the first line: open(my $data, '<', '/etc/hostname') or die "problem: $!"; my @lines = <$data>; my $hostname = $lines[0]; close $data; To compare strings, you don't need always a string comparison operator: if ( $hostname =~ /debian01/ ) { print "OK"; } means: if the string 'debian01' is found somewhere on the first line pulled from the array. Otherwise, it wouldn't work because the "\n" is still in the string. Regards Mirco
From: John W. Krahn on 4 Oct 2006 20:09 mark wrote: > > I have following program in linux: > > open(DATA, "/etc/hostname"); > $hostname = <DATA>; > print $hostname > if ($hostname eq "debian01") { > print "OK"; > } > close(DATA); > > Now, when I execute it I just got: > debian01 > so it seems that $hostdata = "debian01" No, it is actually "debian01\n". You have to use chomp to remove the newline first: perldoc -f chomp John -- Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order. -- Larry Wall
From: Matt Garrish on 4 Oct 2006 20:15 Mirco Wahab wrote: > Thus spoke mark (on 2006-10-05 01:26): > > > I have following program in linux: > > ... > > Now, when I execute it I just got: > > debian01 > > so it seems that $hostdata = "debian01" but there is no 'OK' string :/. > > What is wrong?!? How can I make it working?!? > > > open(DATA, "/etc/hostname"); > > $hostname = <DATA>; > > print $hostname > > if ($hostname eq "debian01") { > > print "OK"; > > } > > close(DATA); > > there are some problems, most > will be found by Perl if you > start your program with: > > use warnings; > use strict; > ... > > To read the content into a scalar, > you could 'slurp' the file, but > for beginners, its better to read > into an array and take the first > element as the first line: > > open(my $data, '<', '/etc/hostname') or die "problem: $!"; > my @lines = <$data>; > my $hostname = $lines[0]; > close $data; > You're assuming that there's more in the file and that he's trying to slurp (I don't see $/ being set to undef in his code). It's common practice to read the first line into a scalar if, for example, the file contains a count / timestamp / some other single value that's being persisted. Using an array is just wasteful extra step, even for beginners. > To compare strings, you don't need > always a string comparison operator: > > if ( $hostname =~ /debian01/ ) { > print "OK"; > } > > means: if the string 'debian01' is found > somewhere on the first line pulled from > the array. Otherwise, it wouldn't work > because the "\n" is still in the string. > Or you just chomp your line and use an equality check. You're contradicting what you wrote above by giving this advice, though. Regular expression checking requires much more careful consideration than most beginners generally give them, so if you're assuming a beginner audience it seems odd you'd provide this example instead of just mentioning chomp (for example, what happens to the OP if the first line actually contains "debian011a"). Matt
From: Tad McClellan on 4 Oct 2006 20:35
mark <mkazmierski(a)gmail.com> wrote: > I have following program in linux: > > open(DATA, "/etc/hostname"); You are missing a check of the return value to see if you actually got what you asked for. > $hostname = <DATA>; You are missing a chomp(). > print $hostname You are missing a semicolon. -- Tad McClellan SGML consulting tadmc(a)augustmail.com Perl programming Fort Worth, Texas |