http://qs321.pair.com?node_id=684935

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

Hey all,

I've been finding that a lot, I mean a lot of employers in the web-engineering business want you to know some Perl. So, I figured, why not learn a bit of it?

I've successfully yelled, "Hello world!" and looked at some Perl syntax. It looks a lot like PHP, a language whose basics I know.

I do, however, have 2 questions.

1) What can Perl do that PHP can't do? I've read threads on this site comparing the two, but all I've seen is that Perl can do more. But what is this magical stuff?

2) Compiling to .exe.... what happens if I just print something with the executable (print "hello")? How do I compile to a chinsy, good looking, GUI app?

Thanks for your help,
-Spear


Update: Thanks a lot. This has cleared some things up for me =)

Replies are listed 'Best First'.
Re: Just starting Perl
by moritz (Cardinal) on May 06, 2008 at 13:44 UTC
    1) What can Perl do that PHP can't do?
    Nothing, basically. Both languages are Turing complete, so from a theoretical point of view you can solve all solvable problems with both languages.

    The important difference is how things are done. I don't know enough about PHP so I can't tell you, but I think I recall that PHP has no namespaces, which can lead to nasty name clashes if you import two modules that use the same names.

    Compiling to .exe.... what happens if I just print something with the executable (print "hello")?

    Compiling to an executable is not officially supported, but there are tools like par that do that.

    How do I compile to a chinsy, good looking, GUI app?
    You have to use a GUI toolkit. TIMTOWTDI, there's Tk, wxperl, GTK bindings etc.
Re: Just starting Perl
by amarquis (Curate) on May 06, 2008 at 13:57 UTC

    1) As stated above, it's not about what you do, it's about how you do it, and Perl is all about "Easy things are easy, difficult things are possible." As far as advantages over PHP, you'll find a great friend in CPAN, which holds solutions to 99.9% of common problems and a shocking number of obscure ones too.

    PHP's benefit is "super easy deployment." It's practically married to MySQL, almost all shared hosting has it, you can start writing a database driven web application very quickly.

    Perl is a little harder, as less shared hosts have it, but you are rewarded for your efforts with more power under the hood. For example, PHP has deep MySQL integration, but with CPAN modules and Perl you can easily interface with almost any way of storing data imaginable.

    Speaking of data, a good example of power delivered easily is DBM::Deep, which you can find on CPAN. It seamlessly ties your data structures to a very fast flat file database that supports transactions. It has found a home in many of my projects I would have used something more complicated for in the past. See also the modules for easily parsing CSV, XML, and other common formats (aren't all our lives better when we never have to write code to parse CSV again?)

    2) Also as stated above, you can write a GUI application with Perl, but it really isn't Perl's strength. I often end up doing all of the messy stuff in Perl (data extraction, analysis, output) and the visualization in something else. Your mileage may vary.

    Edit: Everybody is mentioning CPAN as the magic (and it is magic), but the Perl language brings you a lot of power itself. You said that Perl syntax looked a lot like PHP to you, and it is true you can code Perl in much the same way as you do PHP (or C, or ...). Perl gives you many options as to how to code. As you learn more Perl, though, you'll start to pick up some very powerful Perl idioms and Perl ways of doing complicated tasks with very little code. Take a look at some of the solutions posted by the veterans here in Seekers of Perl Wisdom for examples.

Re: Just starting Perl
by TGI (Parson) on May 06, 2008 at 17:11 UTC

    No one has mentioned PerlApp for building executables yet. I have had very good luck with it.

    The best thing Perl offers that PHP lacks is a pair of pragmata: strict and warnings. CPAN, the 'Inline' modules, closures, sane sorting functions, POE, and flexible, evolving OOP are neat-o and glorious things, but when I do any PHP hacking I miss strictures most of all.

    As others have said, there are a variety of GUI libraries out there. I've used Tk and Wx. Each can be frustrating, and each has it's advantages. Search this site for "which gui" to read lots of discussion on this topic.

    Update: I dug a bit and found some more nodes that my search missed:


    TGI says moo

      I wish I could upvote that post more than once ;-)

      Missing strictures are one of the most annoying "features" that a programming language can have. It's the reason why I don't like javascript, matlab, lua and PHP.

      And closures are one of the things that I didn't miss at first because I couldn't even think of such a thing. Now I'm used to it, and I'm amazed at how much you can do with them.

Re: Just starting Perl
by whakka (Hermit) on May 06, 2008 at 16:15 UTC
Re: Just starting Perl
by citromatik (Curate) on May 06, 2008 at 14:02 UTC
Re: Just starting Perl
by cdarke (Prior) on May 06, 2008 at 14:02 UTC
    PHP isn't just for web applications, it can be used as a shell script (from the "command line"), just like Perl, but very few people use it like that. Perl and PHP syntax differs in many subtle ways, string comparison operators, for example.

    Perl scores on several points, most obvious is CPAN.
Re: Just starting Perl
by regexes (Hermit) on May 06, 2008 at 14:44 UTC
    Hello,

    As for compiling... since no one mentioned it, you might want to look into perl2exe. It's not "free" but there is a free trial version.

    It can compile your programs on most platforms. I've used it a few times and had nice results.

    regexes
Re: Just starting Perl
by Anonymous Monk on May 06, 2008 at 19:20 UTC

    Regarding GUI app with Perl, your question is really a two-parter:

    1. Which toolkit do I use? -- Tough question without a really good answer. Tk is regarded as simple and easy for small stuff, but it's also ancient and (from what I can tell) poorly-documented. gtk2-perl hasn't generated a lot of excitement, but it's probably the closest thing there is to a standard. wx looks a lot like Windows programming and is huge. qt has licensing issues if you ever want to write proprietary software.
    2. How do I compile my GUI program to an exe? -- In my experience, this isn't done very often. The most common scenario is to just make a list of dependencies and have your users install them before running your script. They can install the deps either using the CPANPLUS (or CPAN) shell, or else using their OS's package management system.

    Modern Perl 5 is fun and fairly easy -- as long as you stay away from some pointy corners like typeglobs and subroutine prototypes. And tied variables (yikes!). Oh, and threads. Did you ever have that dream where you're in class in front of everyone, and you notice it's a bit too quiet so you look up from your laptop and realize that everyone is just beginning to notice that you used threads and are having some kind of synchronization problem and you're thinking you might use pdb and the beads of sweat start running down your forehead?

      but it's also ancient and (from what I can tell) poorly-documented

      I beg to differ.

      Tk's recent update is Dec 2007, which isn't "old". The ToolKit has been around for a while (since about 91), but so has wx (92), and I prefer something that is being developed for awhile rather than a new toolkit that might still have some wrinkles and dark corners.

      Tk is also as well documented (as any other Perl module), and has a whole book dedicated to it (Mastering Perl/Tk, ISBN 1565927168), which isn't something other Perl GUI ports/bindings has. It's also very Perlish in its API and very comfortable to work with in a Perl environment.

      Stop saying 'script'. Stop saying 'line-noise'.
      We have nothing to lose but our metaphors.

      Typo: s/pdb/perl -d/
Re: Just starting Perl
by apl (Monsignor) on May 06, 2008 at 13:53 UTC
    If I understand correctly, PHP only runs on Web pages. The Perl Debugger and the ability to run interactively means you can more thoroughly test and debug programs.

    Perl programs can run as daemons, background processes.

    The huge number of libraries in CPAN mean a Perl programmer can do credit-card validations or statistical analysis, produce graphs, perform date validation or calculation, or manipulate Word documents / Excel spreadsheets / XML files, produce documentation from comments, or ... out-of-the-box.

Re: Just starting Perl
by mscharrer (Hermit) on May 06, 2008 at 13:47 UTC
    1) While PHP is only for CGI, Perl is a general language which can be used to program almost everything. I for example use a lot of perl which has nothing to do with WWW/CGI, e.g. code generation for hardware development, simulation logfile analysis, etc..
    Have a look to the Comprehensive Perl Archive Network (CPAN), there you can see how many open libraries exists for Perl.
    I personally compare Perl more with Java - not that they are very similar, but they have some common attributes: Both compile to byte code, both need executable installed (Java VM; Perl Interpreter), both have large set of libraries.

    2) For GUI I know only Perl/TK.

      1) While PHP is only for CGI

      Not quite true. I know some people who write command line scripts in PHP.

      But it's certainly true that the emphasize of PHP is much more on server scripting, whereas perl is more viewed as a general purpose language.