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.


In reply to Problem with "use constant" and UDP socket connection by shockers

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.