f



'use' changed to 'require'

Hi

I've changed
use Socket;
to
require Socket;

in a subroutine since I use it infrequently

I'm getting the error message

Bareword "AF_INET" not allowed while "strict subs"

What have I done?

Regards
John


0
John
1/14/2010 1:46:33 PM
comp.lang.perl.misc 33233 articles. 1 followers. brian (1246) is leader. Post Follow

6 Replies
555 Views

Similar Articles

[PageSpeed] 9

"John" <john1949@yahoo.com> wrote:
>I've changed
>use Socket;
>to
>require Socket;
>
>in a subroutine since I use it infrequently
>
>I'm getting the error message
>
>Bareword "AF_INET" not allowed while "strict subs"
>
>What have I done?

Isn't that obvious? You changed 
	use Socket;
to
	require Socket;
;-))

Seriously, you are missing the import() part of use(), see 
	perldoc -f use

jue 
0
J
1/14/2010 2:04:42 PM
"J�rgen Exner" <jurgenex@hotmail.com> wrote in message 
news:1v8uk51i808hfbe9f7h9atf577fa98eokn@4ax.com...
> "John" <john1949@yahoo.com> wrote:
>>I've changed
>>use Socket;
>>to
>>require Socket;
>>
>>in a subroutine since I use it infrequently
>>
>>I'm getting the error message
>>
>>Bareword "AF_INET" not allowed while "strict subs"
>>
>>What have I done?
>
> Isn't that obvious? You changed
> use Socket;
> to
> require Socket;
> ;-))
>
> Seriously, you are missing the import() part of use(), see
> perldoc -f use
>
> jue

I've added Socket->import(); but same error - John



0
John
1/14/2010 2:25:29 PM
"John" <john1949@yahoo.com> wrote:
>"J�rgen Exner" <jurgenex@hotmail.com> wrote in message 
>> "John" <john1949@yahoo.com> wrote:
>>>I've changed
>>>use Socket;
>>>to
>>>require Socket;
>>>
>>>in a subroutine since I use it infrequently
>>>
>>>I'm getting the error message
>>>
>>>Bareword "AF_INET" not allowed while "strict subs"
>>>
>>>What have I done?
>>
>> Isn't that obvious? You changed
>> use Socket;
>> to
>> require Socket;
>> ;-))
>>
>> Seriously, you are missing the import() part of use(), see
>> perldoc -f use
>
>I've added Socket->import(); but same error - John

Oh, yeah, of course, sorry. The "Bareword ..." message is a compile time
error while the require/import would be done at run time, i.e. way
later.

Not sure what to do about it, I'm not familiar with Socket or what
AF_INET is. I guess maybe you could use a fully qualified name. 

jue
0
J
1/14/2010 2:36:20 PM
>>>>> "JE" == J�rgen Exner <jurgenex@hotmail.com> writes:

  JE> "John" <john1949@yahoo.com> wrote:

  >> I've added Socket->import(); but same error - John

  JE> Oh, yeah, of course, sorry. The "Bareword ..." message is a compile time
  JE> error while the require/import would be done at run time, i.e. way
  JE> later.

  JE> Not sure what to do about it, I'm not familiar with Socket or what
  JE> AF_INET is. I guess maybe you could use a fully qualified name. 

AF_INET is a constant that shouldn't be needed in common socket
code. he should be using IO::Socket instead which has a better API and
less need of obscure constants.

the OP should post the whole section of code with the socket stuff. and
if he does need it, he can't do require and even import since the import
has to be done at compile time for the constant to be declare properly
and then parsed that way in his code. putting the import into a BEGIN
would only help if the module were also loaded there but that becomes
use again.

another solution is to move all the code that involves the socket stuff
to its own module, call use Socket in there, and load his module on
demand.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------
0
Uri
1/14/2010 2:42:20 PM
On 01/14/2010 08:25 AM, John wrote:
> "J�rgen Exner" <jurgenex@hotmail.com> wrote in message 
> news:1v8uk51i808hfbe9f7h9atf577fa98eokn@4ax.com...
>> "John" <john1949@yahoo.com> wrote:
>>> I've changed
>>> use Socket;
>>> to
>>> require Socket;
>>>
>>> in a subroutine since I use it infrequently
>>>
>>> I'm getting the error message
>>>
>>> Bareword "AF_INET" not allowed while "strict subs"
>>>
>>> What have I done?
>> Isn't that obvious? You changed
>> use Socket;
>> to
>> require Socket;
>> ;-))
>>
>> Seriously, you are missing the import() part of use(), see
>> perldoc -f use
>>
>> jue
> 
> I've added Socket->import(); but same error - John
> 
> 
> 

Treat AF_INET as a function--which it is: AF_INET().

Or you can place a forward declaration near the top of your program:

sub AF_INET;


0
Mumia
1/14/2010 3:32:08 PM
"John" <john1949@yahoo.com> writes:

> I've changed
> use Socket;
> to
> require Socket;
>
> in a subroutine since I use it infrequently
>
> I'm getting the error message
>
> Bareword "AF_INET" not allowed while "strict subs"
>
> What have I done?

You changed "use" to "require". They don't do the same thing; more to
the point, the latter does not call "import," which is why the AF_INET
symbol didn't get imported into your name space.

For details, have a look at the docs for the functions you're using:

    perldoc -f use
    perldoc -f require

sherm--
0
Sherm
1/14/2010 3:41:59 PM
Reply: