Re: Perl XS Beginners

[email protected] ("Matthew Horsfall (alh)")
Newsgroups perl.xs
Message-ID <CAJ0K8bjZAa1S3XfEfhwDQQRrTekgpXE5FfwxgwXA9jzS0Lq5qg@mail.gmail.com>
On Fri, Dec 13, 2013 at 2:37 PM, Mike Flannigan <[email protected]> wrote:
>
> I for one had to look up what XS is:
> http://en.wikipedia.org/wiki/XS_%28Perl%29
>
> It is a an interface through which a program written
> in the Perl programming language can call a C or C++
> language subroutine.
>
> I don't know enough about C or C++ to answer why a
> 'system' command or 'exec' command wouldn't do the
> same thing.

XS is how you can interface with other libraries written in C/C++.

For example, DBD::mysql interfaces with the MySQL Client libraries
which are written in C.
You wouldn't want to have to call system('mysql -e ...') or
exec('mysql -e ...') to invoke the command line client every time you
wanted to issue a SQL query, the overhead would be outrageous.

Also, many C libraries don't have command line interfaces that you can
call directly.

It's also how you write Perl modules in C that are generally much
faster than their Pure-Perl variants, think JSON::XS vs JSON::PP.

Another use of XS is to write Perl modules that make use of parts of
perl's internals that aren't exposed in Pure-Perl land, like
Devel::SawAmpersand.

Hope that helps.

-- Matthew Horsfall (alh)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.