in reply to Problem with "use constant" and UDP socket connection

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

Replies are listed 'Best First'.
Re^2: Problem with "use constant" and UDP socket connection
by shockers (Acolyte) on Oct 20, 2005 at 23:21 UTC
    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. :-)