From: Mirco Wahab on
Thus spoke Matt Garrish (on 2006-10-05 02:15):
> Mirco Wahab wrote:
>> Thus spoke mark (on 2006-10-05 01:26):
>> > I have following program in linux:
>> > ...
>> open(my $data, '<', '/etc/hostname') or die "problem: $!";
>> my @lines = <$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.

You are correct, of course - but what I
sometimes found is the mistake of genera-
lizing later the reading of <> into a scalar
(as the OP did here - and what worked here).

To make things more explicit, I provided
the disputed example.

>> 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.

Actually, when I wrote the response,
I had the line

chomp($hostname = $lines[0]);

but dropped that in favor of
the /searchterm/ notation, which I
believed to be much more general here
and in the near future, especially if
you want /i and more ...

> 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").

I tried to make a proper decision but may have failed
or somehow taken the wrong turn, ok. But starting
from the /searchterm/, it is imho straightforward
to jump softly into regular expressions by gradually
adding ...$/ and similar things.

Regards & Thanks

Mirco
From: mark on
Hello,

Thank you - chomp was the thing I needed :) .

Regards, mark