Learn Regex or Perl Frist?

Discussion in 'Perl Misc' started by Don Kim, Nov 16, 2003.

  1. Don Kim

    Don Kim Guest

    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
    Don Kim, Nov 16, 2003
    #1
    1. Advertising

  2. Don Kim

    Ben Morrow Guest

    "Don Kim" <> wrote:
    > 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

    --
    don't get my sympathy hanging out the 15th floor. you've changed the locks 3
    times, he still comes reeling though the door, and soon he'll get to you, teach
    you how to get to purest hell. you do it to yourself and that's what really
    hurts is you do it to yourself just you, you and noone else *
    Ben Morrow, Nov 16, 2003
    #2
    1. Advertising

  3. On Sun, 16 Nov 2003 09:26:11 -0800
    "Don Kim" <> wrote:
    > 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.
    James Willmore, Nov 16, 2003
    #3
  4. Don Kim

    Don Kim Guest

    "Ben Morrow" <> wrote in message
    news:bp8cas$hf7$...
    > 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
    Don Kim, Nov 16, 2003
    #4
  5. Don Kim

    Don Kim Guest

    "James Willmore" <> wrote in message
    news:...
    > 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
    Don Kim, Nov 16, 2003
    #5
  6. Don Kim wrote:
    > 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
    Jürgen Exner, Nov 16, 2003
    #6
  7. Don Kim <> wrote:
    > "Ben Morrow" <> wrote in message
    > news:bp8cas$hf7$...
    >> 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?



    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/


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Nov 16, 2003
    #7
  8. Don Kim

    Ben Morrow Guest

    "Don Kim" <> wrote:
    > "Ben Morrow" <> wrote in message
    > news:bp8cas$hf7$...
    > > 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?


    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

    --
    perl -e'print map {/.(.)/s} sort unpack "a2"x26, pack "N"x13,
    qw/1632265075 1651865445 1685354798 1696626283 1752131169 1769237618
    1801808488 1830841936 1886550130 1914728293 1936225377 1969451372
    2047502190/' #
    Ben Morrow, Nov 16, 2003
    #8
  9. On Sun, 16 Nov 2003 11:30:58 -0800
    "Don Kim" <> wrote:
    > "James Willmore" <> wrote in message
    > news:...

    <snip>
    > 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.

    <snip>
    > 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"
    James Willmore, Nov 17, 2003
    #9
  10. Ben Morrow wrote:
    >
    > 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
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Nov 17, 2003
    #10
  11. Don Kim

    Uri Guttman Guest

    >>>>> "JWK" == John W Krahn <> writes:

    JWK> Ben Morrow wrote:
    >>
    >> 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'.


    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

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Nov 17, 2003
    #11
  12. Uri Guttman wrote:
    >
    > >>>>> "JWK" == John W Krahn <> writes:

    >
    > JWK> Ben Morrow wrote:
    > >>
    > >> 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'.

    >
    > 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
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Nov 17, 2003
    #12
  13. Don Kim

    Uri Guttman Guest

    >>>>> "JWK" == John W Krahn <> writes:

    JWK> Uri Guttman wrote:
    >>
    >> >>>>> "JWK" == John W Krahn <> writes:

    >>

    JWK> Ben Morrow wrote:
    >> >>
    >> >> 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'.

    >>

    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 ) ;


    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

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Nov 17, 2003
    #13
  14. * Don Kim <>:
    > "James Willmore" <> wrote in message
    > news:...


    [...]
    > > 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,
    --
    Iain.
    Iain Truskett, Nov 18, 2003
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kasek
    Replies:
    2
    Views:
    421
    Kevin Spencer
    Jan 30, 2004
  2. Porky Pig Jr
    Replies:
    3
    Views:
    1,017
    Fuzzyman
    May 12, 2004
  3. Steven T. Hatton
    Replies:
    7
    Views:
    346
    Dave Vandervies
    Sep 9, 2005
  4. CoreyWhite
    Replies:
    1
    Views:
    641
    JohnQ
    Mar 31, 2007
  5. Alexander
    Replies:
    20
    Views:
    1,027
    BGB / cr88192
    Sep 11, 2010
Loading...

Share This Page