Learn Regex or Perl Frist?

D

Don Kim

I'm looking to learn perl. Comming from a c++ background, I'm not finding
it very hard to learn, except for getting used to deciphering terse regex
expressions.

Curious, is it better to learn regex first, from say a book like "Mastering
Regular Expressions" by Friedl, then learn perl?

Thx.

Don
 
B

Ben Morrow

Don Kim said:
I'm looking to learn perl. Comming from a c++ background, I'm not finding
it very hard to learn, except for getting used to deciphering terse regex
expressions.

Curious, is it better to learn regex first, from say a book like "Mastering
Regular Expressions" by Friedl, then learn perl?

My recommendation would be: first get the hang of the language,
treating a regex as a black box. When you find that you need to
understand or construct one yourself, read perldoc perlretut or
chapter 5 of the Camel ('Programming Perl', L. Wall, T. Christiansen &
J. Orwant; O'Reilly).

Ben
 
J

James Willmore

I'm looking to learn perl. Comming from a c++ background, I'm not
finding it very hard to learn, except for getting used to
deciphering terse regex expressions.

Curious, is it better to learn regex first, from say a book like
"Mastering Regular Expressions" by Friedl, then learn perl?

If you want to practice with regular expressions in a C++ environment,
then might I suggest pcre++ (http://www.daemon.de/en/software/pcre/).
It's Perl Compatible Regular Expressions in C++. This way, you can
learn Perl Compatible Regular Expressions in C++ :)

If you want to learn Perl, start using the 'perldoc' utility first
(type 'perldoc perldoc' and 'perldoc perl' at the command line to
start). Then, if you want a more extensive look at the language,
you'll a least know what to look for in a book you purchase. Why
throw down $30+ if you don't have to :)

Being an owner of the book mentioned I can say this - unless you're
going to be doing what the author has done with regular expressions,
save your money. If you *must* have a book, get the pocket book by
the same title ($12(?) versus $30(?) for the full book).

That's my $0.02

--
Jim

Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.

a fortune quote ...
Nobody said computers were going to be polite.
 
D

Don Kim

Ben Morrow said:
My recommendation would be: first get the hang of the language,
treating a regex as a black box.

Hmm. Does that mean there are functions or modules in perl that encapsulate
regex calls?

For example, it is encouraged and considered good practice to use standard
c++ libraries for things like arrays and linked lists. For example, you
would use #include<list> in the std namespace to implement a linked list for
your application, rather than rolling one on your own. It is in a sense, a
"black box" for commonly used algorithms and DS for c++.

Does this same analogy hold for Perl with respect to regex?

Thanks for your time.

Don
 
D

Don Kim

James Willmore said:
If you want to practice with regular expressions in a C++ environment,
then might I suggest pcre++ (http://www.daemon.de/en/software/pcre/).
It's Perl Compatible Regular Expressions in C++. This way, you can
learn Perl Compatible Regular Expressions in C++ :)

Thanks! I also looked at the regex library in boost and one called "Greta"
from a guy in microsoft. How does this compare to those? Assuming you've
looked at them.
Being an owner of the book mentioned I can say this - unless you're
going to be doing what the author has done with regular expressions,
save your money. If you *must* have a book, get the pocket book by
the same title ($12(?) versus $30(?) for the full book).

Actually, I already purchased the book, but I got it used for $12 so it was
worth it. I hear you though, I'm sick of buying books only to find it
wasn't useful to me. :-(

Don
 
J

Jürgen Exner

Don said:
I'm looking to learn perl. Comming from a c++ background, I'm not
finding it very hard to learn, except for getting used to deciphering
terse regex expressions.

Curious, is it better to learn regex first, from say a book like
"Mastering Regular Expressions" by Friedl, then learn perl?

The one has little to do with the other. Perl offers also nice arithmetic
functions.

Depending on what you want to do with Perl you may need arithmetic functions
or you may need REs.
The main difference is that you learned about arithmetic in school, but you
didn't learn about REs in school. Therefore plus and minus seem to be more
natural to you.

Make up your mind what kind of programs you want to write. If all you ever
do is numerical calculations then you don't need any REs. If you are going
to write a lot of text processing programs then sooner or later you will
want to learn about REs and you automatically will.

jue
 
T

Tad McClellan

Don Kim said:
Hmm. Does that mean there are functions or modules in perl that encapsulate
regex calls?


Sure, but that isn't (I think) what Ben meant.

He meant don't _try_ to understand the terse regexs for now, just
use them.

(and be sure to reverse that when you are writing production code. :)

You can do lots of useful stuff while learning Perl even if your
programs don't need or use regexs.

(more accurately: if they don't use one of the pattern matching
operators. "Regexes" don't _do_ anything, "operators" do things).

Does this same analogy hold for Perl with respect to regex?


You can write modules (Perlspeak for "libraries") to do
just about anything, including pattern matching. See:

http://search.cpan.org/search?query=regex&mode=all

and

http://search.cpan.org/~abigail/Regexp-Common-2.113/
 
B

Ben Morrow

Don Kim said:
Hmm. Does that mean there are functions or modules in perl that encapsulate
regex calls?

For example, it is encouraged and considered good practice to use standard
c++ libraries for things like arrays and linked lists. For example, you
would use #include<list> in the std namespace to implement a linked list for
your application, rather than rolling one on your own. It is in a sense, a
"black box" for commonly used algorithms and DS for c++.

Does this same analogy hold for Perl with respect to regex?

Hmmm.... in a way. Perl is a higher-level language than C++, and so
there are more things in the core language than in C++. So, Perl has a
basic data type of 'array', which covers pretty much all uses of
arrays/vectors/lists/queues/stacks in C++.

Regexen are part of the language in the same way. The basic unit in
Perl is the 'scalar', which is a polymorphic type: a little like a C
union, except that it is converted between the different
representations as necessary. So if you treat a scalar like a string,
it'll be a string; if you treat it like a number it'll be a number;
etc.

One of the things it can be is a regex, and there is a special
operator '=~' in Perl which means 'match this regex against this
string'. This is something that will perhaps take a little getting
used to coming from C++: Perl is a very operator-rich language, and a
lot of the time it makes more sense to consider your own functions as
operators rather than function calls.

Ben
 
J

James Willmore

Thanks! I also looked at the regex library in boost and one called
"Greta" from a guy in microsoft. How does this compare to those?
Assuming you've looked at them.

I looked over pcre++. I've done more in Perl than C++. The
construction of a regex in pcre++ and Perl are about the same. I
can't speak of performance. I didn't run benchmarks, so I'm not 100%
sure.

Actually, I already purchased the book, but I got it used for $12 so
it was worth it. I hear you though, I'm sick of buying books only
to find it wasn't useful to me. :-(

At least it wasn't full boat ;-)

--
Jim

Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.

a fortune quote ...
Deliberation, n.: The act of examining one's bread to determine
which side it is buttered on. -- Ambrose Bierce, "The Devil's
Dictionary"
 
J

John W. Krahn

Ben said:
One of the things it can be is a regex, and there is a special
operator '=~' in Perl which means 'match this regex against this
string'.

Actually =~ is the binding operator and if used with tr/// there is no
regex involved.


John
 
U

Uri Guttman

JWK> Actually =~ is the binding operator and if used with tr/// there is no
JWK> regex involved.

that is a good point that is not mentioned often enough. but what does
this do?

'abc' =~ ( foo() . $bar ) ;

uri
 
J

John W. Krahn

Uri said:
JWK> Actually =~ is the binding operator and if used with tr/// there is no
JWK> regex involved.

that is a good point that is not mentioned often enough. but what does
this do?

'abc' =~ ( foo() . $bar ) ;

Something similar to:

'abc' =~ /@{[ foo() ]}$bar/ ;


John
 
U

Uri Guttman

JWK> Actually =~ is the binding operator and if used with tr/// there is no
JWK> regex involved.
JWK> Something similar to:

JWK> 'abc' =~ /@{[ foo() ]}$bar/ ;

correct (except for the context. in mine foo() is called in scalar
context and yours calls it in list context).

my point is that =~ is the binding operator but given just an expression
on the right side (no //, m//, s/// or tr///) it will assume m// for the
expression and compile it as such. so =~ has some regex flavor to it as
well as binding. but i do dislike it being called the regex op (mostly
by newbies who don't know about tr/// and that it is not a regex op).

uri
 
I

Iain Truskett

* Don Kim said:
[...]
Being an owner of the book mentioned I can say this -
unless you're going to be doing what the author has done
with regular expressions, save your money. If you
*must* have a book, get the pocket book by the same
title ($12(?) versus $30(?) for the full book).
Actually, I already purchased the book, but I got it used
for $12 so it was worth it. I hear you though, I'm sick
of buying books only to find it wasn't useful to me. :-(

http://iain.truskett.id.au/MiniOwlBook for my view on the
pocket reference.

As you have "Mastering Regular Expressions" (there are 2
editions and I'm not sure which you have, but for beginners
both are good) you have one of the best introductions to
regex around. If you can follow the book, you'll not only
know regex, you'll _understand_ regex and how they work and
why they give the results they do. Which is far better than
just knowing the syntax.

If you're going to learn Perl, it's good to learn regex as
well. Most tutorials cover them fairly early on.

If you're going to learn regex, you may as well learn Perl
as it's a great language to use to play with regex, and has
one of the best implementations.


cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top