Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Linking to Perl from Pascal

by Anonymous Monk
on May 15, 2004 at 13:45 UTC ( #353626=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi:
I'm a pascal programmer. I use FreePascal, wich is multiplataform, almost as Perl. FPC (FreePascal) can link programs with external libraries (.so, .dll, .obj, etc.). The only thing that a pascal programer needs is a library with the C calling convention as an interface.

My question to the great gurus at the monastery is : Can I link my program to a perl module ?

I have to solve this problem: my program writes csv files. Excel can read this files, but I canīt put colours or formulas in my csv file. I saw a perl module, Spreadsheet::WriteExcel. Maybe porting the source code of this module to pascal is a good idea, but I have no time to do this now. So, I would like to use this great module to write my own xls files, without the need of text2xls.pl frontend.

Thank you very much, you powerpuf gurus...

janitored by ybiC: Retitle from "Linking" because one-word nodetitles are wrongjustwrong, and minor format tweaks for legibility

Replies are listed 'Best First'.
Re: Linking
by Fletch (Chancellor) on May 15, 2004 at 13:53 UTC

    Perl has its own C API for extensions called XS. You'd have to implement this from the Pascal side to call perl modules (not to mention the rest of the Perl C API that the module might call). What would be more likely to be a workable solution would be to write a C extension that creates a perl interpreter and link against that from your Pascal (and then you could send perl code from your pascal to load the module and do all the xls mucking). See perldoc perlembed for more details.

Re: Linking
by PodMaster (Abbot) on May 15, 2004 at 13:50 UTC
Re: Linking
by Limbic~Region (Chancellor) on May 15, 2004 at 14:10 UTC
    Anonymous Monk,
    It isn't exactly free, but Extending and Embedding Perl is probably a worthwhile investment for you. The Embedding part is focused on C programs, but you did mention just needing a library of C calling conventions.

    Cheers - L~R

Re: Linking to Perl from Pascal
by calin (Deacon) on May 15, 2004 at 19:10 UTC

    Instead of "hard" (in-process) linking which is segfault prone and hard to do right the first time, I would suggest splitting the Perl job into a separate process. There are at least two ways to do this:

    • Write your spreadsheet formatter as a separate Perl program and feed it data through standard input (see perldoc perlopentut or the IPC modules). You need too define an unobfuscated data representation format ; XML might help.
    • Make your Pascal program write code "on the fly" into a string, and then feed it to the Perl interpreter. This might be easier to prototype, but less consistent and harder to debug.

    Update: braino - Perl code is the callee, not the caller, so the doc pointers are not immediately relevant. Good material for reading, though.

Re: Linking to Perl from Pascal
by jmcnamara (Monsignor) on May 15, 2004 at 22:35 UTC

    In general terms it is probably possible to link a Perl program into Pascal but it probably isn't worth doing it just to get at Spreadsheet::WriteExcel.

    If you need to create an Excel file with formatting and formulas then there are easier alternatives such as creating a HTML file or an XML file.

    Excel can read HTML tables and it preserves the cell formatting and interpolates any formulas in the text. It will read anything from a very simple <table> structures to its own HTML format.

    See Two-dimensional array to "Excel" format for an example.

    The XML format used by Excel 2002 and later is called SpreadsheetML and the specification and schemas are available here. The XML format is relatively easy to handle. The Spreadsheet::WriteExcelXML module uses this approach.

    As an aside, I wrote Spreadsheet::WriteExcel and as it happens I wrote one of its simpler precursors in Pascal. :-)

    --
    John.

      > As an aside, I wrote Spreadsheet::WriteExcel and as it happens I wrote one of its simpler precursors in Pascal. :-)
      Could you show me some code? I was reading all the MS specifications on excel files (BIFF, XML, etc.), and I figured out that the worst work is to write all the headers, data structures and constants. With all this stuff in my hand, it would be easy to port SpreadSheet::WriteExcel back to Pascal.

      I use the freepascal compiler. A unit similar to Spread... would be a useful contribution.

      my email is inpromptu at operamail dot com

        FYI -- you can download his code from CPAN (see search.cpan.org or perl -MCPAN -e 'shell'), without asking for it... it's pretty easy, try it out!
Re: Linking to Perl from Pascal
by dimar (Curate) on May 15, 2004 at 19:58 UTC
    Depending on which version of Excel you have, you can just spit out a spreadsheet directly using XMLSS format. Search for "XMLSS" on Google if you dont know what I am talking about. If all you are doing is outputting colored spreadsheets then you can do that directly from Pascal using plain old XMLSS. Just one more option to consider.
Re: Linking to Perl from Pascal
by boby_drack (Acolyte) on May 16, 2004 at 01:26 UTC
    Hi ! I've seen your message and i'm very surprising when you say that you use 'fpc' (freePascal). Just for your consideration i've found a good compiler more professional that fpc.. so go www.gnupascal.com and you 'll be surprise to use a compiler whose accept 'é' in your program for example sur. for you question on Perl module, i'm sorry i don't know how to do your thing..good luck !
    that many ways to do it (sic !)
      I've tried gnupascal, but sometimes i need to look inside the source code. And theres is the problem with gnupascal: it is written in C, not Pascal. FPC is written in Pascal, and compiled with FPC, so, the development, workarounds and patches are easier to write on FPC. By the way... is it possible to write Perl in Perl ?
Re: Linking to Perl from Pascal
by Anonymous Monk on May 17, 2004 at 14:01 UTC
    Thank you very much !

    All your suggestions are useful. All of them are good approaches. I think that the perldoc and perlembed are the right starting point. Nevertheless, I would try to work around the others solutions.

    Thank you !

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://353626]
Approved by tye
Front-paged by tye
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2018-07-21 12:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (448 votes). Check out past polls.

    Notices?