"hello world" using SWIG or inline.pm ?

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default "hello world" using SWIG or inline.pm ?

    Can somebody teach me how to mix perl code with c++ ?

    That is, how can I use "inline.pm" or the SWIG package to

    write a c++ program which takes a string from perl and prints it ,
    and vice-versa ?

    Thanks.
    thewhizkid Guest

  2. Similar Questions and Discussions

    1. Rendering child controls "inline"
      Hi, I'm currently creating a very basic UserControl. The control implements an interface called IAdvanceView which looks like below Public...
    2. cfinput required="yes" and inline javascript conflict
      Has anyone experienced a situation where inline Javascript is causing Cold Fusion's native error reporting to fail. I have a script that tests 2...
    3. SWIG and "connect"? [Was: SWIG and char * parameters]
      In Message-Id: <200307241723.41949.ben@thingmagic.com> Ben Giddings <ben@thingmagic.com> writes: Maybe missing the point but.... 1. Are you...
    4. #22197 [Fbk->NoF]: "inline" functions cannot use "static" identifier
      ID: 22197 Updated by: sniper@php.net Reported By: j_vanveelen at jea dot ca -Status: Feedback +Status: ...
    5. #22197 [Asn->Fbk]: "inline" functions cannot use "static" identifier
      ID: 22197 Updated by: sniper@php.net Reported By: j_vanveelen at jea dot ca -Status: Assigned +Status: ...
  3. #2

    Default Re: "hello world" using SWIG or inline.pm ?

    Also sprach thewhizkid:
    > Can somebody teach me how to mix perl code with c++ ?
    >
    > That is, how can I use "inline.pm" or the SWIG package to
    >
    > write a c++ program which takes a string from perl and prints it ,
    > and vice-versa ?
    You can't use Inline for calling Perl from C++. If you want that, read
    'perldoc perlembed' and 'perldoc perlcall'.

    For the other direction, you have many possibilities:

    Here's a couple of examples with C. C++ is only interesting if you want
    to deal with C++ objects.

    #! /usr/bin/perl -w

    use Inline C;

    hello('world');

    __END__
    __C__
    void hello (SV *msg) {
    char *text = SvPV_nolen(msg);
    printf("hello, %s\n", text);
    }

    The above explicitely passes a SV* (a Perl scalar) to hello() and then
    retrieves the string stored inside. 'perldoc perlapi' shows the
    available macros and functions provided by the Perl API.

    Inline::C can also do some automatic type conversions for you:

    #! /usr/bin/perl -w

    use Inline C;

    hello('world');

    __END__
    __C__
    void hello (char *msg) {
    printf("hello, %s\n", msg);
    }

    Here the transition from SV* to char* happens implicitely. This
    automatism however only works for the basic C types. If you have a
    library with complex types, you need to define your own conversion
    routines. This is usally done in a file called 'Typemap' (they are
    only sparesly documented so you might need google and some patience to
    find out how they work).

    Another alternative way is using pure XS. Inline::C/C++ are just
    wrappers around XS. They look simpler but they aren't really. XS has the
    advantage that development happens in a more standard way: You work on
    the .xs file and use another console to do a 'make'. Errors are thus
    appearing on your console and not dumped in files in a hidden directory
    (as Inline does it).

    When working with XS, you usually have the overhead of creating a real
    module. But that's not really a bad thing. You start with h2xs:

    ethan@ethan:~/Projects$ h2xs -c -b 5.5.3 -n Hello::World
    Writing Hello/World/ppport.h
    Writing Hello/World/World.pm
    Writing Hello/World/World.xs
    Writing Hello/World/Makefile.PL
    Writing Hello/World/README
    Writing Hello/World/t/1.t
    Writing Hello/World/Changes
    Writing Hello/World/MANIFEST

    This creates a skeletal module with all required files. The -b switch
    adds a compatibility layer to your module so that the newer portions of
    the PerlAPI will be made compatible with at least 5.00503 (for this
    case).

    After that, you add the functionality to Hello/World/World.xs. Right now
    it only contains:

    #include "EXTERN.h"
    #include "perl.h"
    #include "XSUB.h"

    #include "ppport.h"


    MODULE = Hello::World PACKAGE = Hello::World

    Adding an XS function that prints its argument as string looks like:

    void
    hello(string)
    SV *string;
    PPCODE:
    printf("hello, %s\n", SvPV_nolen(string));

    You put this below the 'MODULE =' line. Now you go to the other console
    and do a 'perl Makefile.PL && make':

    ethan@ethan:~/Projects/Hello/World$ perl Makefile.PL && make
    Checking if your kit is complete...
    Looks good
    Writing Makefile for Hello::World
    cp World.pm blib/lib/Hello/World.pm
    AutoSplitting blib/lib/Hello/World.pm (blib/lib/auto/Hello/World)
    /usr/bin/perl /usr/lib/perl5/5.8.0/ExtUtils/xsubpp -typemap
    /usr/lib/perl5/5.8.0/ExtUtils/typemap World.xs > World.xsc && mv
    World.xsc World.c
    Please specify prototyping behavior for World.xs (see perlxs manual)
    cc -c -I. -O3 -march=athlon -fno-strict-aliasing -D_LARGEFILE_SOURCE
    -D_FILE_OFFSET_BITS=64 -O2 -DVERSION=\"0.01\" -DXS_VERSION=\"0.01\"
    -fpic "-I/usr/lib/perl5/5.8.0/i686-linux/CORE" World.c
    Running Mkbootstrap for Hello::World ()
    chmod 644 World.bs
    rm -f blib/arch/auto/Hello/World/World.so
    LD_RUN_PATH="" cc -shared -L/usr/local/lib World.o -o
    blib/arch/auto/Hello/World/World.so
    chmod 755 blib/arch/auto/Hello/World/World.so
    cp World.bs blib/arch/auto/Hello/World/World.bs
    chmod 644 blib/arch/auto/Hello/World/World.bs
    Manifying blib/man3/Hello::World.3

    And now, you can already call your function without installing the
    module:

    ethan@ethan:~/Projects/Hello/World$ perl -Mblib -MHello::World
    Hello::World::hello("world");
    __END__
    hello, world
    ethan@ethan:~/Projects/Hello/World$

    If you do a 'make install' you have this module properly installed
    system-wide.

    Since it's a proper module, you have a file Hello/World/World.pm, too.
    This is an ordinary Perl module file. If you want your function to be
    exported automatically, you do it there:

    @EXPORT = qw(hello);

    After doing 'make' again (whenever you change something, do a 'make'),
    you call it thusly:

    ethan@ethan:~/Projects/Hello/World$ perl -Mblib -MHello::World
    hello("world");
    __END__
    hello, world
    ethan@ethan:~/Projects/Hello/World$

    In the above command-lines, -Mblib tells perl to use the blib/ directory
    as location of the module. blib/ holds the compiled but not yet
    installed module.

    XS does the same conversions as Inline::C, so the function hello() could
    also be written as:

    void
    hello(string)
    char *string;
    PPCODE:
    printf("hello, %s\n", string);

    Returning values from a function isn't hard either:

    char *
    hello_concat(string)
    char *string;
    PREINIT:
    char *retstring;
    int len;
    CODE:
    len = strlen(string) + 8;
    New(0, retstring, len, char);
    memcpy(retstring, "hello, ", 7);
    memcpy(retstring + 7, string, strlen(string));
    retstring[len] = 0;
    RETVAL = retstring;
    OUTPUT:
    RETVAL

    Just as you can pass in Perl types and do the conversion yourself, you
    can use the Perl stack explicitely to return a SV. That way, you can
    return lists of values by XPUSH()ing them and then telling perl how many
    values you pushed with XSRETURN(num_of_values):

    void
    hello_stack(string)
    char *string;
    PREINIT:
    SV *retval;
    PPCODE:
    retval = newSVpv("hello, ", 7);
    SvGROW(retval, strlen(string));
    sv_catpv(retval, (const char*)string);

    /* sv_2mortal() makes the SV mortal:
    * that is, it will auto-destroy itself when it goes
    * out of scope */
    XPUSHs(sv_2mortal(retval));
    XSRETURN(1);

    I can't much comment on SWIG. From the XS/Inline/SWIG trio I'd say it is
    the worst choice since it's not Perl specific. It may make the first
    steps of wrapping a library into a Perl module easier, but you have
    better control over what happens when using Inline or XS. If you ignore
    the common myths that Inline is much easier than all the rest, XS is
    still the best way of accessing C/C++ from Perl.

    The manpages you should read are:

    perlxstut /* a tutorial, not complete but helpful nonetheless */
    perlguts /* explains the concepts behind the Perl internals */
    perlxs
    perlcall /* if you call Perl code from your XS */
    perlapi
    perlapio /* the I/O related PerlAPI */

    Tassilo
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus}) !JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexi ixesixeseg;y~\n~~dddd;eval
    Tassilo v. Parseval Guest

  4. #3

    Default Help : from file system structure to topic maps format

    Hello,



    I want to make a little script in perl .
    .... but I'm very newbie in this langage



    For each sub-directory of a given directory ...

    (and for each file in the directory)

    .... I want to write in a file something like that :




    < <topic>
    <baseName>
    <baseNameString>nameOfTheSubDirectory</baseNameString>
    </baseName>
    <occurrence>
    <resourceRef
    xlink:href="file:///fullNameOfTheFirstFileInTheSubDirectory"/>
    </occurrence>
    <occurrence>
    <resourceRef
    xlink:href="file:///fullNameOfTheSecondInTheSubDirectory"/>
    </occurrence>
    ...
    <occurrence>
    < resourceRef
    xlink:href="fille:///nameOfTheLastFileInTheSubDirectory"/ >
    </occurrence>
    </topic>



    (Where fullNameOfEachFile is the name of the file with his path)



    Any idea to help me to start ?



    Regards



    Bernard



    test Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139