Click to See Complete Forum and Search --> : Perl pisses me off sometimes


syn
01-23-2002, 01:55 PM
I have the hardest time learning the most obscure things in perl, well obscure to me. Ive almost completed this simple script that adds a host to a .rhosts file on multiple machines.

#!/usr/bin/perl -w
use strict;
use diagnostics;

my @hosts = qw(
host1
host2
host3
);

my $num = scalar(@hosts);
# print "$num\n"; # for testing

print "\nEnter hostname: ";
my $hostname = <STDIN>;
chomp $hostname;

print "\nEnter login: ";
my $login = <STDIN>;
chomp $login;

for (my $i = 0; $i < $num; $i++) {
# check if entry exists in the .rhosts file first
my $rval1 = `rsh $hosts[$i] -l ops grep $hostname .rhosts` || die "check error: $!";
if ($? == 0 ) { # return value is zero if it finds something, so dont write
print "$hostname already exists on this machine.\n";
print "$rval1\n";
} else { # return value is something else if it doesnt find it, so write
my $rval2 = `rsh $hosts[$i] -l ops echo $hostname $login >> .rhosts` || die "write error: $!";
# give the user some feedback
print "Adding entry to .rhosts on $hosts[$i].\n";
print "$rval2\n";
}
}

print "\nAll Finished.\n";



Am I properly getting the return value of grep? if there is already a host that exists it will tell me, but as soon as I make it search for an entry that isnt there it craps out and gives me:
Uncaught exception from user code:
check error: Illegal seek at addrhosts.pl line 27, <STDIN> line 2.
Heh, once I get this working it could be a good example for http://codeexamples.org maybe. Anyways what do you think?

jemfinch
01-23-2002, 02:31 PM
If Perl angers you, choose a more well-behaved language, like Python.

Jeremy

syn
01-23-2002, 02:59 PM
Ouch! lol, Im not *really* pissed off. I try not to give up once I really get into something, its just sometimes I need a little help kicking down the walls if you know what I mean. I do intend to learn python as soon as I feel that I could do damn near anything I want in perl first. From the examples Ive seen I think it would be good to learn OO stuff with python. Not to mention the pygames stuff ;-) Anyone else have any comments on my nasty code?

takshaka
01-24-2002, 05:33 AM
my $rval1 = `rsh $hosts[$i] -l ops grep $hostname .rhosts` || die "check error: $!";


You are dying when grep returns nothing; ie, when no matches are found. So, er, don't do that :)

kmj
01-24-2002, 10:35 AM
wow, perl actually has exceptions now?

Strike
01-24-2002, 03:06 PM
Originally posted by kmj:
<STRONG>wow, perl actually has exceptions now?</STRONG>

Not really, I don't think. die has always been in there, but it basically exits the whole script (afaik), it doesn't really raise a catchable exception

kmj
01-24-2002, 03:37 PM
Originally posted by Strike:
<STRONG>Not really, I don't think. die has always been in there, but it basically exits the whole script (afaik), it doesn't really raise a catchable exception</STRONG>

Oh, maybe I just never paid attention to the return values.

takshaka
01-24-2002, 10:01 PM
Of course die is catchable. Perl does exception handling with eval.

jemfinch
01-25-2002, 04:53 AM
Originally posted by takshaka:
Of course die is catchable. Perl does exception handling with eval.

The funny part is that they call it "exception handling," as if it's comparable
to exceptions in other languages. :)

Jeremy

takshaka
01-25-2002, 07:00 PM
Imagine that: Jeremy has something bad to say about Perl.

You realize that you have become a caricature of yourself, don't you?

YaRness
01-25-2002, 07:59 PM
i just wanna say:

python sucks.

but only to irritate jeremy.

jemfinch
01-25-2002, 09:08 PM
Originally posted by takshaka:
Imagine that: Jeremy has something bad to say about Perl.

You realize that you have become a caricature of yourself, don't you?

I can't say I quite understand what you mean by "a caricature of yourself."

Anyway, Perl's exception handling via eval/die is notably inferior to exception
handling in languages that actually have exceptions. In languages that support
real exceptions, you can raise an exception of any value -- in Perl, an
"exception" can only be raised as a string.

Jeremy

Gnu/Vince
01-25-2002, 09:22 PM
Originally posted by jemfinch:
<STRONG>I can't say I quite understand what you mean by "a caricature of yourself."


</STRONG>

Maybe he refers to the (old) time when you were a Perl advocate and said OO and Python sucked.

jemfinch
01-25-2002, 09:23 PM
Originally posted by Gnu/Vince:
Maybe he refers to the (old) time when you were a Perl advocate and said OO and
Python sucked.

If that's the case, he certainly should realize that some people do, in fact, correct
their incorrect viewpoints and grow.

Jeremy

Gnu/Vince
01-25-2002, 09:26 PM
Originally posted by jemfinch:
<STRONG>If that's the case, he certainly should realize that some people do, in fact, correct
their incorrect viewpoints and grow.

Jeremy</STRONG>

Check this: http://www.linuxnewbie.org/cgi-bin/ubbcgi/ultimatebb.cgi?ubb=get_topic&f=14&t=000811

takshaka
01-26-2002, 02:29 AM
Jeremy said:
I can't say I quite understand what you mean by "a caricature of yourself."

caricature n. A representation, especially pictorial or literary, in which the subject's distinctive features or peculiarities are deliberately exaggerated to produce a comic or grotesque effect.

I couldn't care less that you've changed your tastes or that you label this change growth. In fact, I think you're a well-studied, smart guy, and I agree with a lot of your assertions.

But dropping in non sequiturs and wild opinions as if they had any relevance just makes you look like another unreliable zealot, good for little more than a point and chuckle.

In languages that support
real exceptions, you can raise an exception of any value -- in Perl, an "exception" can only be raised as a string.

See, this is a perfectly legitimate statement unfortunately posted the day after the "natural languages (http://www.linuxnewbie.org/cgi-bin/ubbcgi/ultimatebb.cgi?ubb=get_topic&f=14&t=004386)" sophistry and in the same thread where you (jokingly, but no less predictably) suggest to a person asking for Perl help that he switch languages.

Sheesh. That was a long way of saying you are predictable to the point of absurdity.