What's going on is that the sys/ioctl.ph file (at least the one on my system) defines a bunch of subs, however, it does not use something like Exporter to do so, it just defines them in whatever the current package happens to be. Also, require only loads a file once: if you have require 'sys/ioctl.ph' in your package main (i.e. test.pl) that gets called first, then the require 'sys/ioctl.ph' in package funnybusiness has no effect, and therefore, the sub funnybusiness::FIONREAD never gets defined.

There are several possible solutions, which one is most "elegant" depends... One possible solution might be to use do instead of require, because it always runs the file, no matter how many times it's been run before, but keeping in mind the error checking described in the documentation of do. Also, I'd move that do outside of the sub s1, there's no need for the file to get loaded every time the sub is called. As an alternative to do, one could do delete $INC{'sys/ioctl.ph'}; require 'sys/ioctl.ph'; to make sure the require loads the file (see %INC).

Yet another solution might be to do the following in one of the library files:

{ package Sys::Ioctl; BEGIN { delete $INC{'sys/ioctl.ph'}; require 'sys/ioctl.ph'; } }

And then to use Sys::Ioctl::FIONREAD() everywhere. This could also be separated out into its own Sys/Ioctl.pm file, which could in turn use Exporter to get those definitions exported in the usual manner. This is probably the solution I would favor in a larger application.


In reply to Re: require in script breaks module by haukex
in thread require in script breaks module by chrstphrchvz

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.