in reply to Re^4: inline in a perl module
in thread inline in a perl module

Hi, First of all sorry for not wrapping the code with
. I think that the problem resides with the "END" directive. but I don't understand where I should use it and how. Can you please take a quick glance at the following code ? Thanks, Nir <code> package NewModule; use 5.008005; use strict; use warnings; use lib '/usr/lib/perl5/vendor_perl/5.8.0'; use lib '/users/nirf/perlTrials/cpanModulesInstallation/lib/perl5/site +_perl/5.8.0'; use lib '/users/nirf/perlTrials/cpanModulesInstallationInline/lib/perl +5/site_perl/5.8.5'; use Inline CPP => Config => INC => '-l/users/nirf/perlTrials/inlineExa +mple'; use Inline CPP => Config => MYEXTLIB => '/users/nirf/perlTrials/inline +Example/libmain.so'; use Inline CPP => Config => ENABLE => STD_IOSTREAM; #use Inline CPP; require Exporter; our @ISA = qw(Exporter); # Items to export into callers namespace by default. Note: do not expo +rt # names by default without a very good reason. Use EXPORT_OK instead. # Do not simply export all your public functions/methods/constants. # This allows declaration use NewModule ':all'; # If you do not need this, moving things directly into @EXPORT or @EXP +ORT_OK # will save memory. our %EXPORT_TAGS = ( 'all' => [ qw( ) ] ); our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); our @EXPORT = qw( ); our $VERSION = '0.01'; __END__ __CPP__ + #include "main.h" int CCC; char GlobalArr[13] = {'G','l','o','b','a','l',' ','a','r','r','a',' +y','\0'}; MyClass myGlobalInstance(GlobalArr) ; int add(int x, int y) { char arr[7]={'i','n',' ','a','d','d','\0'}; MyClass mc(arr); myGlobalInstance.wow(); return x + y; } 1; END # Below is stub documentation for your module. You'd better edit it! =head1 NAME NewModule - Perl extension for blah blah blah =head1 SYNOPSIS use NewModule; blah blah blah =head1 DESCRIPTION Stub documentation for NewModule, created by h2xs. It looks like the author of the extension was negligent enough to leave the stub unedited. Blah blah blah. =head2 EXPORT None by default. =head1 SEE ALSO Mention other useful documentation such as the documentation of related modules or operating system documentation (such as man pages in UNIX), or any relevant external documentation such as RFCs or standards. If you have a mailing list set up for your module, mention it here. If you have a web site set up for your module, mention it here. =head1 AUTHOR Nir Frenkel, E<lt>nirf@localdomainE<gt> =head1 COPYRIGHT AND LICENSE Copyright (C) 2008 by Nir Frenkel This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.5 or, at your option, any later version of Perl 5 you may have available. =cut

Replies are listed 'Best First'.
Re^6: inline in a perl module
by syphilis (Archbishop) on May 19, 2008 at 09:21 UTC
    I still think there's too many unknowns for us to be of much help. What happens when you run the following script (called try.pl):
    use strict; use warnings; use Inline CPP => <<'EOC'; #include "main.h" int CCC; char GlobalArr[13] = {'G','l','o','b','a','l',' ','a','r','r','a',' +y','\0'}; MyClass myGlobalInstance(GlobalArr) ; int add(int x, int y) { char arr[7]={'i','n',' ','a','d','d','\0'}; MyClass mc(arr); myGlobalInstance.wow(); return x + y; } EOC print add(7, 5);
    For me, I get the following fatal errors:
    try_pl_13b2.xs:15:18: main.h: No such file or directory try_pl_13b2.xs:18: error: `MyClass' does not name a type try_pl_13b2.xs: In function `int add(int, int)': try_pl_13b2.xs:22: error: `MyClass' was not declared in this scope try_pl_13b2.xs:22: error: expected `;' before "mc" try_pl_13b2.xs:23: error: `myGlobalInstance' was not declared in this +scope dmake: Error code 129, while making 'try_pl_13b2.o'
    But then I don't have 'main.h' so at least some of those failures are not surprising.

    If I ever find out what 'main.h' contains (and where it is located), why/if those 'use lib' directives are necessary, and why/if those 'use Inline CPP => Config' parameters are necessary, then I might be able to offer better help. (And the same might hold for others, too. I'm actually fairly hopeless when it comes to C++, and I hope it remains that way ... so you might not get much help from me, in any event :-)

    One thing I did notice which I think is almost certainly incorrect is:
    use Inline CPP => Config => INC => '-l/users/nirf/perlTrials/inlineExa +mple';
    Did you mean:
    use Inline CPP => Config => INC => '-I/users/nirf/perlTrials/inlineExa +mple';
    Cheers,
    Rob
      Hi Rob, from the cpp and header I created a shared object. Than I called that shared oblect from test.pl file. The test.pl file checks that the shared object is ok (and it worked). Than I wanted to have the same thing inside a pm file (and it just does't go for me). And this is the reason for my current problems. As i sad, wjen calling that shared object from a simple pl file everything works fine. The output of the pl file is -------------------outout of test.pl----------------
      I'm constructing : Global array I'm constructing : in add I am inside wow Destruction : in add 9 + 16 = 25 Destruction : Global array
      -----------------main.h-----------------------
      #ifndef _nir_test_main #define _nir_test_main #include <string> using namespace std; class MyClass { public: MyClass(const char*); ~MyClass(); int wow(); private : string _my_favorite_argument; }; #endif
      -----------------main.cpp-----------------------
      #include <stdio.h> #include "main.h" //extern int funcOuter(int); MyClass::MyClass(const char * a_my_favorite_argument) { printf("I'm constructing : %s \n",a_my_favorite_argument); _my_favorite_argument.append(a_my_favorite_argument); } MyClass::~MyClass() { printf("Destruction : %s \n",_my_favorite_argument.c_str()); } int MyClass::wow() { printf ("I am inside wow \n"); return 12 / 3; } int func(const int&); int main (int argc, char* argv[]) { //calling to a function from another so //funcOuter(4); printf ("hello \n"); int x = 10; func(x); return 0; } int func (const int& x) { printf ("in func : %d \n",x); return 0; }
      ------------------test.pl----------------------------
      #!/usr/bin/perl -w use lib '/usr/lib/perl5/vendor_perl/5.8.0'; use lib '/users/nirf/perlTrials/cpanModulesInstallation/lib/perl5/site +_perl/5.8.0'; use lib '/users/nirf/perlTrials/cpanModulesInstallationInline/lib/perl +5/site_perl/5.8.5'; #use lib '/users/nirf/perlTrials/cpanModulesInstallationParseRecDescen +t/lib/perl5/site_perl/5.8.5'; #use Inline Config => LIBS => '-L/users/nirf/perlTrials/inlineExample' +, LIBS => '-lmain'; #use Inline Config => LIBS => '-L/users/nirf/perlTrials/inlineExample +-lmain'; use Inline CPP => Config => INC => '-l/users/nirf/perlTrials/inlineExa +mple'; use Inline CPP => Config => MYEXTLIB => '/users/nirf/perlTrials/inline +Example/libmain.so'; + use Inline CPP; + sub func { add(1,1); } print "9 + 16 = ", add(9, 16), "\n"; + __END__ __CPP__ + #include "main.h" char GlobalArr[13] = {'G','l','o','b','a','l',' ','a','r','r','a',' +y','\0',}; MyClass myGlobalInstance(GlobalArr) ; int add(int x, int y) { char arr[7]={'i','n',' ','a','d','d','\0'}; MyClass mc(arr); myGlobalInstance.wow(); return x + y; }
      Thanks, Nir
        I think you really ought to take a look at line 15 of NewModule_b1a3.xs - as was suggested some time back by samtregar, and perhaps others (though, admittedly, not very forcefully by myself). It may just be a matter of missing newlines - but the XS file will show it up best.

        I can generate the same error as you got with:
        use warnings; use Inline CPP; greet(); __END__ __CPP__ void greet() { printf("Hello World\n"); }
        (where there's no newline at the bottom of the script) but adding just one newline at the bottom of the script fixes the issue.

        I suspect that the newline is being lost in the module - but seeing what's at line 15 of NewModule_b1a3.xs should enable us to confirm that. Perhaps that closing "1;" and the pod documentation need to be moved up above the "__END__" marker (or before the "__CPP__" marker) so that the pm file terminates with a newline immediately after the end of the cpp code.

        Cheers,
        Rob
Re^6: inline in a perl module
by moritz (Cardinal) on May 19, 2008 at 09:00 UTC
    I'd try the following:
    # perl code here 1; __END__ # Documentation here __CPP__ // CPP code here // no END at the end.
      Hi moritz, I thank you again for your efforts. I changes the order of those macros but it still gives me and error :
      cp lib/NewModule.pm blib/lib/NewModule.pm PERL_DL_NONLAZY=1 /usr/bin/perl "-MExtUtils::Command::MM" "-e" "test_h +arness(0, 'blib/lib', 'blib/arch')" t/*.t t/NewModule....NOK 1# Failed test (t/NewModule.t at line 9) # Tried to use 'NewModule'. # Error: BEGIN not safe after errors--compilation aborted at /use +rs/nirf/perlTrials/NewModule/blib/lib/NewModule.pm line 12. # Compilation failed in require at (eval 1) line 2. # Looks like you failed 1 tests of 1. t/NewModule....dubious Test returned status 1 (wstat 256, 0x100) DIED. FAILED test 1 Failed 1/1 tests, 0.00% okay Failed Test Stat Wstat Total Fail Failed List of Failed ---------------------------------------------------------------------- +--------- t/NewModule.t 1 256 1 1 100.00% 1 Failed 1/1 test scripts, 0.00% okay. 1/1 subtests failed, 0.00% okay. make: *** [test_dynamic] Error 2
      ***************** The NewModule.pm is : *****************
      package NewModule; use 5.008005; use strict; use warnings; use lib '/usr/lib/perl5/vendor_perl/5.8.0'; use lib '/users/nirf/perlTrials/cpanModulesInstallation/lib/perl5/site +_perl/5.8.0'; use lib '/users/nirf/perlTrials/cpanModulesInstallationInline/lib/perl +5/site_perl/5.8.5'; use Inline CPP => Config => INC => '-l/users/nirf/perlTrials/inlineExa +mple'; use Inline CPP => Config => MYEXTLIB => '/users/nirf/perlTrials/inline +Example/libmain.so'; use Inline CPP => Config => ENABLE => STD_IOSTREAM; use Inline CPP; require Exporter; our @ISA = qw(Exporter); # Items to export into callers namespace by default. Note: do not expo +rt # names by default without a very good reason. Use EXPORT_OK instead. # Do not simply export all your public functions/methods/constants. # This allows declaration use NewModule ':all'; # If you do not need this, moving things directly into @EXPORT or @EXP +ORT_OK # will save memory. our %EXPORT_TAGS = ( 'all' => [ qw( ) ] ); our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); our @EXPORT = qw( ); our $VERSION = '0.01'; + 1; __END__ __CPP__ + #include "main.h" int CCC; char GlobalArr[13] = {'G','l','o','b','a','l',' ','a','r','r','a',' +y','\0'}; MyClass myGlobalInstance(GlobalArr) ; int add(int x, int y) { char arr[7]={'i','n',' ','a','d','d','\0'}; MyClass mc(arr); myGlobalInstance.wow(); return x + y; } # Below is stub documentation for your module. You'd better edit it! =head1 NAME NewModule - Perl extension for blah blah blah =head1 SYNOPSIS use NewModule; blah blah blah =head1 DESCRIPTION Stub documentation for NewModule, created by h2xs. It looks like the author of the extension was negligent enough to leave the stub unedited. Blah blah blah. =head2 EXPORT None by default. =head1 SEE ALSO Mention other useful documentation such as the documentation of related modules or operating system documentation (such as man pages in UNIX), or any relevant external documentation such as RFCs or standards. If you have a mailing list set up for your module, mention it here. If you have a web site set up for your module, mention it here. =head1 AUTHOR Nir Frenkel, E<lt>nirf@localdomainE<gt> =head1 COPYRIGHT AND LICENSE Copyright (C) 2008 by Nir Frenkel This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.5 or, at your option, any later version of Perl 5 you may have available. =cut
        I changes the order of those macros
        But not in the way I suggested.

        And don't show us the code of the test outputs if the module doesn't even build - show us the the compilation error.

        Ok, so you try to compile your documentation as C++ code. Doesn't usually work, I think...

        I don't know that Inline stuff, but I bet there must be a way to tell it where the inlined code ends and your documentation starts. Or just throw that docs out, they aren't helpfull at all in this state.


        Search, Ask, Know