shockers has asked for the wisdom of the Perl Monks concerning the following question:
but if instead I try to use a variable (set to a passed-in argument) for REMOTE_HOST:my $udp_server = shift @ARGV; my $trans_serv; my $remote_host; my $remote_port; my $destination; if ( $udp_server ) { use Socket; use constant SIMPLE_UDP_PORT => 4001; use constant REMOTE_HOST => 'myservername'; # use constant REMOTE_HOST => $udp_server; $trans_serv = getprotobyname('udp'); $remote_host = gethostbyname(REMOTE_HOST); $remote_port = SIMPLE_UDP_PORT; $destination = sockaddr_in($remote_port, $remote_host); socket (UDP_SOCK, PF_INET, SOCK_DGRAM, $trans_serv); }
I get the following runtime error:use constant REMOTE_HOST => $udp_server;
There's not a rule that "use constant NAME => value" can't use a variable for the value is there?Bad arg length for Socket::pack_sockaddr_in, length is 0, should be +4 at /opt/perl/lib/5.3.7/i686-linux-thread-multi/Socket.pm line 373
I'm only wanting to pull in Socket if the user wants to send data to one of several servers and would hate to have to do:
The above works, but I'd have to change the program every time the server list changes.my $udp_server = shift @ARGV; if ( $udp_server eq 'server1' ) { use constant REMOTE_HOST => 'se +rver1'; } elsif ( $udp_server eq 'server2' ) { use constant REMOTE_HOST => 'se +rver2'; } elsif ( $udp_server eq 'server3' ) { use constant REMOTE_HOST => 'se +rver3'; } elsif ( $udp_server eq 'server4' ) { use constant REMOTE_HOST => 'se +rver4'; } else { $udp_server = ''; + } if ( $udp_server ) { use Socket; use constant SIMPLE_UDP_PORT => 4001; : :
I'm running ActivePerl 5.8.7 on Linux.
Do you see anything wrong that I'm doing?
Thanks.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Problem with "use constant" and UDP socket connection
by TedYoung (Deacon) on Oct 20, 2005 at 15:51 UTC | |
by shockers (Acolyte) on Oct 20, 2005 at 23:21 UTC | |
by TedYoung (Deacon) on Oct 21, 2005 at 13:14 UTC | |
|
Re: Problem with "use constant" and UDP socket connection
by Fletch (Bishop) on Oct 20, 2005 at 15:59 UTC | |
|
Re: Problem with "use constant" and UDP socket connection
by revdiablo (Prior) on Oct 20, 2005 at 17:00 UTC |