Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Perl & C/C++

by hemaexp26 (Novice)
on Mar 28, 2003 at 07:20 UTC ( #246411=perlquestion: print w/replies, xml ) Need Help??

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

Hello experts,
I am doing my final semester and want to do my project in graphics applications. But i have no idea about my software selection. Some people insisted me to select C/C++. My application simply reads a text file which contains some geometry informations (like 4 points of a rectangle) and displays it in graphical format (stack of rectangles).

When discussing with experts i came to know that C/C++ is a power full language but it lacks in GUI and asked me to select any one of GUI tool kits.I got two GUI tool kits GTK and TK. What is the diffence between these two tools? Both support multi-platform?

As i said earlier my application simply reads a text file and displays it in visual format. I planned to use C/C++ for file processing. But when i studied PERL document (iam new to perl. just now started) i came to know its powerful scripting capabilities. It provides lot of functions for string manipulation, file handling etc.

Can i use Perl instead of C/C++ in my application? I hope it is easy to embedding Perl/tk or Perl/GTK than C++/tk. If that is the case Perl is better choice than C/C++? How do u rank Perl compared to C/C++? Is Perl is more powerfull than C/C++?

Let me clear my doubts.


edited: Fri Mar 28 13:20:06 2003 by jeffa - added <p> tags

Replies are listed 'Best First'.
Re: Perl & C/C++
by zby (Vicar) on Mar 28, 2003 at 08:43 UTC
    I have no experience with GTK, but TK is quite multi-platform (it works on X and it works on Windows - in ActiveState package and with a patch on Cygwin). I cannot compare it to other GUI tool kits, but for me it was a bit buggy - I don't mean anything critical but some inconsequences in appearence.

    If all you need is to display a stack of rectangles you might use just the GD library to generate a picture (a png or a gif).

    The difference between Perl and C/C++ is a bit broad subject and I don't feel competent to talk about it, but for sure you can write applications in Perl, you even can write graphical applications in Perl if only you use a good library for the low level data munging.

Re: Perl & C/C++
by hiseldl (Priest) on Mar 28, 2003 at 14:39 UTC

      Can i use Perl instead of C/C++ in my application?

    Yes, absolutely, Perl will allow you to quickly prototype your data munging application. Unfortunately there are not many GUI building tools for the Perl GUI toolkits. There are a few to choose from, if you are on a windows box, you can use Perl/Tk, wxPerl or the Win32::GUI module. And, if you are on a Linux/*nix box, you can use the previously mentioned, less the Win32 module, or Gtk-Perl. There are other toolkits available, but from what I've seen they are not used much, or not as publicly available.

      Is Perl a better choice than C/C++?

    For a class project, it probably does not make that much difference. For enterprise applications, you will probably end up using C/C++, and for web applications, you will probably end up using Perl. These choices are usually made by non-programmers, although there are a few startups that are run by programmers...I digress. :-)

      How do u rank Perl compared to C/C++?

    Again, for a class project there is not much to compare. The only suggestion I can make is to evaluate what you want to get out of this assignment, i.e. learn a new language, learn to program GUI apps, etc., and then choose from there.

      Is Perl is more powerfull than C/C++?

    According to the turing test for language completeness, no, they are equal. :P

    Good luck with your project.

    What time is it? It's Camel Time!

Re: Perl & C/C++
by submersible_toaster (Chaplain) on Mar 28, 2003 at 13:46 UTC

    zby makes some good points , however has confused me one one issue

    you even can write graphical applications in Perl if only you use a good library for the low level data munging.

    Actually I suggest that perl is the perfect place to do your low level data munging, then begin plugging that data (I suggest you investigate the tutorials , pay particular note to using hashes and hash references) into you selected graphical library. If all you need is an image then probably GD is a good choice, if you're aiming for extra credit - do something funky with GTK or TK (make the rectangles move into place - whatever), my first suggestion would be to investigate Tk::Canvas , CPAN has been mis-behaving lately though so if is unavailable - go googling.

    Not having ever coded much in C or C++ (in fact I have only ever really hacked at existing code) , I suggest that perl can be very forgiving when you have a job that needs results NOW! , c/c++ can be rewarding when your program must produce the results FAST (no matter how long it takes to write).

    Good luck

    I can't believe it's not psellchecked
Prima (Re: Perl & C/C++)
by deadkarma (Monk) on Mar 28, 2003 at 14:44 UTC

    It's another cross-platform GUI toolkit for perl, and it has some nifty image processing functions.

    The examples that come with it are, in my opinion, quite impressive.

Re: Perl & C/C++
by Anonymous Monk on Mar 28, 2003 at 15:54 UTC

    Program in C/C++, it'll take you longer and you'll learn far more. With Perl you don't get to play with neat things like pointers and learn how to avoid buffer overflows. You also don't get to learn how to manage extremely large amounts of code because Perl can be written so compactly and efficiently. There is also far too much solidly tested, freely available, well packaged Perl code. What fun is it if you're not even writing all the code yourself? Perl's also too high level, you aren't forced to do the fun, educational low-level programming everyone dreams about. Perl people are also way too helpful, so you'll be tempted just to take the easy way and ask for help when you get stuck. So as you can see, C/C++ is the obvious choice. I wonder if anyone will take this seriously... Damn you and your non-default theme!

    Oh, and as for the graphical interface (I refuse to say GUI), go with Qt from trolltech. The API rocks, the implementation rocks, the documentation really rocks, it's GPL'd for non-commercial products and if you want to go commercial you just have to buy a developer license. It's also used by many major companies (adobe just released a product using it) and by THE ABSOLUTE BEST unix desktop environment, KDE. And no, I don't work for Trolltech :).

      Qt certainly is one of the nicest GUI products around, and don't forget PerlQt which gives you the best of all worlds:

      You can prototype in PerlQt and quickly convert to C++ if you need to - though on a 700Mhz Athlon (hardly cutting edge anymore :) I've had no need to since speed has not been an issue - you *might* suffer with resource usage though - Perl + PerlQt is a bit of a memory hog.

      PerlQt is certainly good on Unices, and apparently fine on Win32 + Cygwin - watch out for licensing on Win32.

      Finally, you get pretty good documentation and qt-designer. Yes, by all means learn how to code Qt by hand at first, but designer will save you a lot of time if the project grows. Designer is the overriding reason why I stick with Qt - not perfect, but far nicer to use than glade, or that commercial wx GUI designer (sorry, forgotten its name, I quickly gave up after test driving the trial download!)

Re: Perl & C/C++
by hardburn (Abbot) on Mar 28, 2003 at 16:08 UTC

    GTK+ works with Perl. GTK+ works under Win32. But getting GTK with Perl under Win32 is a problem. Last I heard (3-6 months ago), GTK-Perl was still a no-go on Win32.

    I'd look around for wxWindows, which should work with Perl under a lot more platforms than GTK+. My only gripe with this is that I really like Glade (GTK+ WYSIWYG GUI editor), and I haven't found an equivilent tool for wxWindows (I hate actually writing GUI code).

    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

      I really like Glade

      So it's true. These people do exist.

        I'm not saying Glade is the best tool for making GUIs (though I don't know of equivilent tools for GTK+). I'm saying it is a lot better than pounding your head against a hand-coded GUI.

        I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
        -- Schemer

        Note: All code is untested, unless otherwise stated

Re: Perl & C/C++
by zentara (Archbishop) on Mar 28, 2003 at 16:00 UTC
    Hi, I didn't author this, but it will give you a good idea of how easy it would be in perl/tk.

    UPDATE Got rid of huge ugly array.

    #!/usr/bin/perl use Tk; my $STEP = 20; my $BOXSIZE = 70; my $WINDOWX = 700; my $WINDOWY = 500; my $mw = new MainWindow( -title => "This is a test", -width => $WINDOWX, -height => $WINDOWY, ); my $canvas = $mw->Canvas( -bg => "black", -width => $WINDOWX, -height => $WINDOWY )->pack(); $canvas->repeat( $STEP, sub { rect($canvas); } ); $canvas->repeat( $STEP, sub { oval($canvas); } ); MainLoop(); sub oval { my $c = shift; my $x = rand $WINDOWX; my $y = rand $WINDOWY; my $sizex = rand $BOXSIZE; my $sizey = rand $BOXSIZE; my $o = $c->createOval( $x, $y, $x + $sizex, $y + $sizey, -fill => getcolor(), ); push ( @ovals, $o ); while ( @ovals > 1000 ) { $c->delete( shift (@ovals) ); } } sub rect { my $c = shift; my $x = rand $WINDOWX; my $y = rand $WINDOWY; my $size = rand $BOXSIZE; my $r = $c->createRectangle( $x, $y, $x + $size, $y + $size, -fill => getcolor(), ); push ( @rects, $r ); while ( @rects > 1000 ) { $c->delete( shift (@rects) ); } } sub getcolor{ my($string) = '#'; my($length) = 7; my(@chars) = ('a' .. 'f', 0 .. 9); while (length($string) != $length) { $string .= $chars[ rand($#chars) ]; } return $string; }
      Wouldn't it have been simpler to generate that huuge array dynamically as:
      my @COLOR; { local *ARGV; @ARGV = qw(/usr/lib/X11/rgb.txt); while (<>) { push @COLOR, $1 if /\d+\s+\d+\s+\d+\s+(\S+)/; } }
      Or better yet, just generate a random RGB on each hit?

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

        Just wondering... is there any specific reason why you use @ARGV and <>? Methinks it would be cleaner to just open() and close() the file. Also, this way you could choose not to die if the file doesn't exist (e.g. on Windows).

      I'm going to have nightmares about that array.

      Clear separation of data and code is a very, very good thing.

Re: Perl & C/C++
by aquarium (Curate) on Mar 30, 2003 at 13:04 UTC
    You don't need to write code using a full blown GUI toolkit for a bar graph..there are graph modules for perl which make it very easy to construct a bar graph etc. GD::Graph, Chart::Graph to mention two such modules. These modules do use the GUI toolkits underneath, but you don't need to worry about the details too much. You could of course choose C/C++, but why re-invent the wheel. Chris

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://246411]
Approved by Corion
Front-paged by Courage
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2023-06-04 00:10 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (17 votes). Check out past polls.