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

Hi, I copied this sample from: http://search.cpan.org/~ingy/Inline-C-0.76/lib/Inline/C.pod
#!/usr/bin/perl -w use lib '/usr/share/perl5/Module/Install'; use Inline C; print "test\n"; greet('Ingy'); __END__ __C__ void greet(char* name) { printf("Hello %s\n", name); }
changed the file to executable. ran it and got this:
test Undefined subroutine &main::greet called at ./inlineTst.pl line 5.
what am I doing wrong?

Replies are listed 'Best First'.
Re: newbi with inline C
by davido (Cardinal) on Mar 09, 2016 at 16:07 UTC

    It works for me exactly as you pasted it. However, there's a strict violation which is probably not preventing it from working, but still not very nice.

    use strict; and you will see a problem. If that doesn't make it clear enough, also add use diagnostics; You'll see the following:

    Bareword "C" not allowed while "strict subs" in use at mytest.pl line +6. Execution of mytest.pl aborted due to compilation errors (#1) (F) With "strict subs" in use, a bareword is only allowed as a subroutine identifier, in curly brackets or to the left of the "=> +" symbol. Perhaps you need to predeclare a subroutine? Uncaught exception from user code: Bareword "C" not allowed while "strict subs" in use at mytest.pl l +ine 6. Execution of mytest.pl aborted due to compilation errors. shell returned 255

    Change use Inline C; to use Inline 'C';.

    I'd also be curious as to how Inline and Inline::C were installed, and if they pass their test suites on install. It may be they're not correctly installed. It would be hard for the test suite to pass cleanly yet this code snippet not work.


    Dave

Re: newbi with inline C (works)
by tye (Sage) on Mar 09, 2016 at 16:02 UTC

    For what it is worth, that code works for me:

    $ perl ic.pl test Hello Ingy

    Update: Following a suggestion from chatter:

    #!/usr/bin/perl -w use lib '/usr/share/perl5/Module/Install'; use Inline C => Config => BUILD_NOISY => 1, CLEAN_AFTER_BUILD => 0; print "test\n"; greet('Ingy'); __END__ __C__ void greet(char* name) { printf("Hello %s\n", name); }

    yields:

    $ perl ic.pl test Undefined subroutine &main::greet called at ic.pl line 5.

    (Probably because the extra arguments on the 'use' line override the default behavior of look for "__C__".)

    - tye        

      Thanks. It actually helps. It probably means that this cpan module was not installed correctly
Re: newbi with inline C
by runrig (Abbot) on Mar 09, 2016 at 16:07 UTC
    Don't know. "Works for me", but that 'use lib' statement seems unnecessary.
Re: newbi with inline C
by 1nickt (Canon) on Mar 09, 2016 at 15:52 UTC

    Never mind, sorry for the noise.

    use Inline C;

    Never used it but you will have better luck I think with:

    use Inline::C;

    Hope this helps!


    The way forward always starts with a minimal test.