shockers has asked for the wisdom of the Perl Monks concerning the following question:

I have this snippet where it works if I use a constant in "use constant 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); }
but if instead I try to use a variable (set to a passed-in argument) for REMOTE_HOST:
use constant REMOTE_HOST => $udp_server;
I get the following runtime error:
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
There's not a rule that "use constant NAME => value" can't use a variable for the value is there?

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:

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; : :
The above works, but I'd have to change the program every time the server list changes.

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

    use happens at compile time. So it gets executed before your variables are initialized in this case. On top of that, they are also being executed regardless of the conditional in the if statement (i.e. $udp_server is false). Any reason why you want to use constants here? Why not just use variables?

    If you really wanted to use constant here you could do this:

    use constant REMOTE_HOST => shift(@ARGV); my $udp_server = REMOTE_HOST;

    One more note, the use Socket will be executed regardless of $udp_server (so it is always being "pulled in"). That could be changed to require Socket; import Socket if you didn't want it imported if $udp_server is false.

    Ted Young

    ($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)
      I haven't tried your shift(@ARGV) suggestion out, but in my application I can have multiple options -- one of them being to specify a server. (So in the real code, it's more than simply "shift @ARGV").

      As others are very quick to point out that constants are for constants, using "use constant REMOTE_HOST" is not my choice. It's necessary because Socket wants its information that way. What seems unnecessary is not being able to specify a server name through a variable.

      I feared "use Socket" would immediately pull it in, but thought I read somewhere I could do an 'if' around it. Guess not.

      With your example "use constant REMOTE_HOST => shift(@ARGV)", this works at compile time because @ARGV is known at compile time?

        I might be out in left field here but I don't see any requirement that their various functions you are using requires constants. gethostbyname and socket can accept variables. AMAF, there is very, very little difference in passing and variable vs. a constant to a sub.

        use vars; :-)

        Ted Young

        ($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)
Re: Problem with "use constant" and UDP socket connection
by Fletch (Bishop) on Oct 20, 2005 at 15:59 UTC

    You want to use a variable as the value of a constant . . .

    /boggle

    Not really a constant then is it?

Re: Problem with "use constant" and UDP socket connection
by revdiablo (Prior) on Oct 20, 2005 at 17:00 UTC

    As Fletch points out, constants are supposed to be, well, constant. Something that varies is called a variable. Perhaps what you want is a subroutine that returns the current value of your variable? This would let you use it like the constant, but without all the weird contortions:

    my $foo; sub FOO () { $foo } $foo = 1; print FOO, "\n"; $foo = 2; print FOO, "\n";