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

I've re-written a script that tries to waken PC's on our network (I had to as the Getopt::Std module didn't work on my office PC!) and it works perfectly well. Unfortunately I like to use the strict pragma in all my scripts and this re-write will not work with this. If I supply AF_INET and STOCK_DGRAM as the level and optname parameters to setsockopt it fails with the old bareword not allowed error. I thought that if I put these values in variables and passed these to my script this would satisfy the strict pragma. Nope I get the following error :-
Argument "SOCK_DGRAM" isn't numeric in socket at RC_Bad_wakeonlan.pl l +ine 59, <I NF> line 1. Argument "AF_INET" isn't numeric in socket at RC_Bad_wakeonlan.pl line + 59, <INF> line 1. socket : at RC_Bad_wakeonlan.pl line 59, <INF> line 1.
The actual code in my script is :-
my $Domain = "AF_INET" ; my $Type = "SOCK_DGRAM" ; my $Level = 'SOL_SOCKET' ; my $Optname = 'SO_BROADCAST' ; # # Alocate socket and send packet $raddr = gethostbyname($ipaddr); $them = pack_sockaddr_in($port, $raddr); $proto = getprotobyname('udp'); socket(S, $Domain, $Type, $proto) or die "socket : $!"; setsockopt(S, $Level,$Optname, 1) or die "setsockopt : $!"; print "Sending magic packet to $ipaddr:$port with $hwaddr\n";

I can obviously get this to work without using the strict pragma with this code :-
socket(S, AF_INET, SOCK_DGRAM, $proto) or die "socket : $!"; setsockopt(S, SOL_SOCKET, SO_BROADCAST, 1) or die "setsockopt : $! +";
Is my understanding of this wanting? As I say I can get round it but I'm intrigued. I was also looking for the manual entry for setsockopt(2) (as alluded to on page 785 of the "Programming Perl" book but am unable to locate it on any of our servers (the powers that be probably thought these man pages took up too much room) is this documentation available on the net?

Replies are listed 'Best First'.
Re: Can you use setsockopt with the strict pragma?
by davorg (Chancellor) on Oct 25, 2006 at 15:40 UTC

    Your code is equivalent to this:

    socket(S, "AF_INET", "SOCK_DGRAM", $proto) or die "socket : $!"; setsockopt(S, "SOL_SOCKET", "SO_BROADCAST", 1) or die "setsockopt : $!

    The code from the example you quote is this:

    socket(S, AF_INET, SOCK_DGRAM, $proto) or die "socket : $!"; setsockopt(S, SOL_SOCKET, SO_BROADCAST, 1) or die "setsockopt : $!

    There's one obvious difference between the two. Your code passes strings to the functions. The example code passes values which have been defined as numeric constants (in Socket.pm). If they aren't defined correctly in your Socket.pm (as your bareword error might indicate) then it's possible that your Perl installation might be broken.

    So yes, you can use this code with "use strict" but you need to define your variables correctly.

    my $Domain = AF_INET; my $Type = SOCK_DGRAM; my $Level = SOL_SOCKET; my $Optname = SO_BROADCAST;
    I was also looking for the manual entry for setsockopt(2) (as alluded to on page 785 of the "Programming Perl" book but am unable to locate it on any of our servers (the powers that be probably thought these man pages took up too much room) is this documentation available on the net?

    Why not ask Google?

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      I recommend using my $Domain = AF_INET(); because it isn't ambiguous and will tell you that you have a problem even if you didn't use strict;. It also gives a bit of a clearer indication of the problem.

      - tye        

      Thanks - even without the documentation I should have figured that out but failed miserably! In my defence all I can say is that I didn't know what those parameters were for , hence my request for the documentation. (Yip I know that wouldn't bear much scrutiny never mind stand up in court - mea culpa!) I did try google first but could only find rather flimsy entries for Socket nothing for the Socket(2) referred to in the manual. Thanks again for your help.
      Cheers
Re: Can you use setsockopt with the strict pragma?
by djp (Hermit) on Oct 26, 2006 at 04:11 UTC
    You seem to be missing use Socket; in your script:
    $ perl -e 'use strict; setsockopt(S, SOL_SOCKET, SO_BROADCAST, 1);' Bareword "SOL_SOCKET" not allowed while "strict subs" in use at -e lin +e 1. Bareword "SO_BROADCAST" not allowed while "strict subs" in use at -e l +ine 1. Execution of -e aborted due to compilation errors. $ perl -e 'use strict; use Socket; setsockopt(S, SOL_SOCKET, SO_BROADC +AST, 1);' $
    setsockopt might be in section 3 or section 3SOCKET of the man pages. I'd be gobsmacked if your 'powers that be' had removed all man pages from all your (development) systems for space reasons.