How about a class library to marry Perl and C++ 
Author Message
 How about a class library to marry Perl and C++

I love to code in Perl and use it for much of my programming today.
But sometimes it makes sense to code in C, or nowadays in C++.   Now
I've written tons of C, but getting back to coding in it makes me
miss Perl, and the way it gets certain tasks done quickly, and surprisingly
safely.

So what I wish I had was a class library to allow me to, for the short
stretches where I need to, write perl-like stuff in the middle of my
C.   A library with just a few basic types -- scalar, list, hash and file,
with possibly extra types for reference, function and regexp.  And they
would work like the perl types, with arbitrary length strings you can
freely pass around, inherent garbage collection and code that just
doesn't bomb because you overwrote something or played with a pointer.

Now of course there are lots of class libraries out there with variable
length strings, lists, and associative arrays.   And while I haven't looked
at them all, what I've seen hasn't felt like perl to me, which is the goal.

Because I (and zillions of others) know how to think in perl, and how to
work solutions quickly to problems using these types.  We could use a class
library that does the same thing but it would be yet another library to
learn to read and write.   I know about the SPLASH library, but I actually
want code that looks like perl, which means  splice( list, 1, 2, olist )
rather than list.splice( 1, 2 olist ).  The latter is the c++ and OO way of
thinking about it, of course, but the former is more readable to the perl
programmer.

Ideally this code might well just be derived from the existing perl source,
or another class library might be morphed to fit this form.   Mainly I
wanted to see if a lot of other people thought it a good idea, which might
encourage that it get done, either by a longtime perl source hacker or
Larry himself, or by somebody with a library they think is best adapted.

There are lots of questions about how to do it.  Have a true perl scalar
type or accept it's better to break that apart a bit but have lots of
implicit conversion overloads?   Do files the perl way or leave them to
c++?   Do regexps as a class or just have some functions with strings?
What to do about syntactic stuff you just can't get in C++ easily or at all?
(Thinks like initializers, regexps, braces to index hashes, scalar/array
contexts etc.)

Thoughts?  Or does somebody's favourite class library already do this?

--
        Brad Templeton                   http://www.*-*-*.com/



Sat, 10 Aug 2002 03:00:00 GMT  
 How about a class library to marry Perl and C++
<< So what I wish I had was a class library to allow me to, for the short
stretches where I need to, write perl-like stuff in the middle of my C.   A
library with just a few basic types -- scalar, list, hash and file, with
possibly extra types for reference, function and regexp. >>

You have posted to a C++ newsgroup. Most of what you want is already
standard C++, with the exception of regexp, which I also miss when I write
C++.

I wish Larry Wall had written the syntax of Perl more like C or C++, but I
understand he wanted to make the syntax resemble Unix shell syntax. Had he
done this, straight bidirectional conversions would be easier, now that C++
has  a lot of the same abilities.

I also wish someone would create a decent C++ regexp class, similar to
Larry's (which IMHO is better than any of the others). I personally am far
too lazy to do it.

--

Paul Lutus
www.arachnoid.com


Quote:
> I love to code in Perl and use it for much of my programming today.
> But sometimes it makes sense to code in C, or nowadays in C++.   Now
> I've written tons of C, but getting back to coding in it makes me
> miss Perl, and the way it gets certain tasks done quickly, and
surprisingly
> safely.

> So what I wish I had was a class library to allow me to, for the short
> stretches where I need to, write perl-like stuff in the middle of my
> C.   A library with just a few basic types -- scalar, list, hash and file,
> with possibly extra types for reference, function and regexp.  And they
> would work like the perl types, with arbitrary length strings you can
> freely pass around, inherent garbage collection and code that just
> doesn't bomb because you overwrote something or played with a pointer.

> Now of course there are lots of class libraries out there with variable
> length strings, lists, and associative arrays.   And while I haven't
looked
> at them all, what I've seen hasn't felt like perl to me, which is the
goal.

> Because I (and zillions of others) know how to think in perl, and how to
> work solutions quickly to problems using these types.  We could use a
class
> library that does the same thing but it would be yet another library to
> learn to read and write.   I know about the SPLASH library, but I actually
> want code that looks like perl, which means  splice( list, 1, 2, olist )
> rather than list.splice( 1, 2 olist ).  The latter is the c++ and OO way
of
> thinking about it, of course, but the former is more readable to the perl
> programmer.

> Ideally this code might well just be derived from the existing perl
source,
> or another class library might be morphed to fit this form.   Mainly I
> wanted to see if a lot of other people thought it a good idea, which might
> encourage that it get done, either by a longtime perl source hacker or
> Larry himself, or by somebody with a library they think is best adapted.

> There are lots of questions about how to do it.  Have a true perl scalar
> type or accept it's better to break that apart a bit but have lots of
> implicit conversion overloads?   Do files the perl way or leave them to
> c++?   Do regexps as a class or just have some functions with strings?
> What to do about syntactic stuff you just can't get in C++ easily or at
all?
> (Thinks like initializers, regexps, braces to index hashes, scalar/array
> contexts etc.)

> Thoughts?  Or does somebody's favourite class library already do this?

> --
> Brad Templeton http://www.templetons.com/brad/



Sat, 10 Aug 2002 03:00:00 GMT  
 How about a class library to marry Perl and C++

Quote:

><< So what I wish I had was a class library to allow me to, for the short
>stretches where I need to, write perl-like stuff in the middle of my C.   A
>library with just a few basic types -- scalar, list, hash and file, with
>possibly extra types for reference, function and regexp. >>

>You have posted to a C++ newsgroup. Most of what you want is already
>standard C++, with the exception of regexp, which I also miss when I write
>C++.

As I wrote in the message, I know that there are class libraries ranging
from the basic_string and STL templates that are similar but they are
not the same.   Can I write:

        scalar s,s2,s3;
        list l;
        hash h;
        file f;

        s = sprintf( "File %s has size %d\n", filename, size );
        l[10] = 20
        l[5] = "hello";
        h["what" + "is" + "this"] = s;
        splice( l, 4, 2, 100 );
        h["yes"] = &l;            // reference to list l

        open( f, "myfile" );
        while( s = f.getline() ) {
                chomp( s );
                // do stuff
                }
        list( &s, &s2 ) = s3.match( "^(.*)::(.*)$" );

Note that:
        I really can mix types.  Is that a bad idea?  Perhaps, but it's perl.
        I can assign into a list/array beyond its boundaries, and it
        just works.

        I call functions on variables rather than using variable.method() in
        most places.  Yes, that's not c++ that's perl.  I suggest
        variable.method() when c++ syntax won't let me define an operator
        from perl like ~= or <FILENAME> or \reference and so on.

This is not proposed to be C++.  It's meant to be very perl-like.  So
that while writing a C++ program, you can suddenly burst into
pseudo-perl for a few lines of code if that's the best way to get things
done.

Yes, the hash_map template class is not too far off the perl semantics.
--
        Brad Templeton                  http://www.templetons.com/brad/



Sat, 10 Aug 2002 03:00:00 GMT  
 How about a class library to marry Perl and C++


Quote:


> ><< So what I wish I had was a class library to allow me to, for the
short
> >stretches where I need to, write perl-like stuff in the middle of my
C.   A
> >library with just a few basic types -- scalar, list, hash and file,
with
> >possibly extra types for reference, function and regexp. >>

> >You have posted to a C++ newsgroup. Most of what you want is already
> >standard C++, with the exception of regexp, which I also miss when I
write
> >C++.

> As I wrote in the message, I know that there are class libraries
ranging
> from the basic_string and STL templates that are similar but they are
> not the same.   Can I write:

>    scalar s,s2,s3;
>    list l;
>    hash h;
>    file f;

>    s = sprintf( "File %s has size %d\n", filename, size );
>    l[10] = 20
>    l[5] = "hello";
>    h["what" + "is" + "this"] = s;
>    splice( l, 4, 2, 100 );
>    h["yes"] = &l;            // reference to list l

>    open( f, "myfile" );
>    while( s = f.getline() ) {
>            chomp( s );
>            // do stuff
>            }
>    list( &s, &s2 ) = s3.match( "^(.*)::(.*)$" );

_Most_ of this could be easily translated to C++ code with the existing
standard library.  The only really missing functionality, as already
pointed out, is regular expressions.

Yes, the syntax will be different.  This is C++ not Perl.  If you want
Perl, use Perl.  To make this look like Perl you'd have to change the
language, not just the libraries, and there'd be little benefit in the
end.

Quote:
> Note that:
>    I really can mix types.  Is that a bad idea?  Perhaps, but it's

perl.

And it's impossible to do in C++ (note that many such cases can
be "faked" using templates, but not all such cases).

Quote:
>    I can assign into a list/array beyond its boundaries, and it
>    just works.

This is just foolish.  It may be handy in Perl, but it's bad OO design.

Quote:
>    I call functions on variables rather than using variable.method
() in
>    most places.  Yes, that's not c++ that's perl.  I suggest
>    variable.method() when c++ syntax won't let me define an
operator
>    from perl like ~= or <FILENAME> or \reference and so on.

You won't find many proponents to using a procedural approach instead
of an OO approach here.

Quote:
> This is not proposed to be C++.  It's meant to be very perl-like.  So
> that while writing a C++ program, you can suddenly burst into
> pseudo-perl for a few lines of code if that's the best way to get
things
> done.

It's not the "best" way, only the way you're used to.  If we can do the
exact same thing with different syntax, what makes the Perl way
better?  You're really asking for something bizarre here.  Pick a
language and stick with it.  Adding Perl functionality is fine, but
wanting it to look like Perl syntax is not.

BTW, for a good regexp library in C++ look at regexp++.  It will soon
be modified and incorporated into Boost (www.boost.org), so keep a
watch on it as well.  It supports the same Perl regular expressions,
but uses a more appropriate C++ syntax.

Quote:
> Yes, the hash_map template class is not too far off the perl

semantics.

hash_map is not standard C++.

--
William E. Kempf
Software Engineer, MS Windows Programmer

Sent via Deja.com http://www.deja.com/
Before you buy.



Sat, 10 Aug 2002 03:00:00 GMT  
 How about a class library to marry Perl and C++

[...]

Quote:
> So what I wish I had was a class library to allow me to, for the short
> stretches where I need to, write perl-like stuff in the middle of my
> C.   A library with just a few basic types -- scalar, list, hash and file,
> with possibly extra types for reference, function and regexp.  And they
> would work like the perl types, with arbitrary length strings you can
> freely pass around, inherent garbage collection and code that just
> doesn't bomb because you overwrote something or played with a pointer.

[...]

There is a library called "perl-c++-api" written by Ken Fox.
I don't remember exactly where, but you can make a search of the
author at CPAN (I found it last week)

Good Luck.
Eyal.



Sun, 11 Aug 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Perl-like c++ class library available

2. Is there a perl style regular expression library(class) for C and C++

3. Perl-like C++ class library

4. Help - need to convert Unix C++ class library to DOS

5. Class library to make C++ more Perlish?

6. SPLASH 1.90 released c++ class library

7. Perl-like C++ class?

8. Perl XS using C++ classes

9. C++ classes for perl translation

10. Embeding perl in C++ shared library

11. Embeding perl in C++ shared library

12. Examples of Perl 5 Extensions to C++ libraries

 

 
Powered by phpBB® Forum Software