When would you use qr// on a literal string?

Discussion in 'Perl Misc' started by it_says_BALLS_on_your forehead, Jul 17, 2006.

  1. I thought that qr was mainly used for pre-compiling variables into
    regex patterns, but a colleague uses it like so:

    my ( $name, $value ) = split qr/=/, $string;

    Are there any benefits to doing this? He claims that the use of the
    qr// op here can help capture an error if the STRING is not a valid
    regex. Does this make any sense?
     
    it_says_BALLS_on_your forehead, Jul 17, 2006
    #1
    1. Advertising

  2. "it_says_BALLS_on_your forehead" <> wrote in
    news::

    > I thought that qr was mainly used for pre-compiling variables into
    > regex patterns, but a colleague uses it like so:
    >
    > my ( $name, $value ) = split qr/=/, $string;
    >
    > Are there any benefits to doing this? He claims that the use of the
    > qr// op here can help capture an error if the STRING is not a valid
    > regex. Does this make any sense?


    The first argument to split is a regex, whether you use regex notation or
    not.

    So, I am not fond of:

    split '=', $string;

    because it obscures that fact.

    On the other hand, I don't see what additional mileage qr gets you other
    than saying: I really want to signal that this is a regex to other
    programmers.

    As for what mileage that gets you in terms of preventing errors, take a
    look at:

    #!/usr/bin/perl

    use strict;
    use warnings;

    my $data = 'name$sinan';

    my @a = split '$', $data;
    my @b = split /$/, $data;
    my @c = split qr/$/, $data;

    # correct
    my @d = split /\$/, $data;

    print "@a\n@b\n@c\n@d\n";

    __END__

    Sinan
     
    A. Sinan Unur, Jul 17, 2006
    #2
    1. Advertising

  3. it_says_BALLS_on_your forehead

    Dr.Ruud Guest

    it_says_BALLS_on_your forehead schreef:

    > I thought that qr was mainly used for pre-compiling variables into
    > regex patterns, but a colleague uses it like so:
    >
    > my ( $name, $value ) = split qr/=/, $string;
    >
    > Are there any benefits to doing this? He claims that the use of the
    > qr// op here can help capture an error if the STRING is not a valid
    > regex. Does this make any sense?


    I don't see any difference in using /+/ or qr/+/, both give the same
    error.
    My gut feeling says that split does a precompile on a /PATTERN/.


    $ perl -MO=Deparse -e 'print split ".+"'
    print split(/.+/, $_, 0);

    $ perl -wle 'print split "+"'
    Quantifier follows nothing in regex; marked by <-- HERE in m/+ <-- HERE
    / at -e line 1.

    $ perl -wle 'print split /+/'
    Quantifier follows nothing in regex; marked by <-- HERE in m/+ <-- HERE
    / at -e line 1.

    $ perl -wle 'print split qr/+/'
    Quantifier follows nothing in regex; marked by <-- HERE in m/+ <-- HERE
    / at -e line 1.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Jul 17, 2006
    #3
  4. it_says_BALLS_on_your forehead <> wrote:
    > I thought that qr was mainly used for pre-compiling variables into
    > regex patterns, but a colleague uses it like so:
    >
    > my ( $name, $value ) = split qr/=/, $string;
    >
    > Are there any benefits to doing this?



    Not that I can see.


    > He claims that the use of the
    > qr// op here can help capture an error if the STRING is not a valid
    > regex.



    I have no idea what "capture an error" might mean...


    > Does this make any sense?



    No, unless he can give an example where using qr// gives more
    info than an m// with the same pattern.

    These both make the same output for instance.

    perl -e 'qr/(/'

    and

    perl -e 'm/(/'

    Can your colleague provide a counter-example that shows qr// as
    somehow "better"?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jul 17, 2006
    #4
  5. Dr.Ruud wrote:
    > it_says_BALLS_on_your forehead schreef:
    >
    > > I thought that qr was mainly used for pre-compiling variables into
    > > regex patterns, but a colleague uses it like so:
    > >
    > > my ( $name, $value ) = split qr/=/, $string;
    > >
    > > Are there any benefits to doing this? He claims that the use of the
    > > qr// op here can help capture an error if the STRING is not a valid
    > > regex. Does this make any sense?

    >
    > I don't see any difference in using /+/ or qr/+/, both give the same
    > error.
    > My gut feeling says that split does a precompile on a /PATTERN/.
    >
    >
    > $ perl -MO=Deparse -e 'print split ".+"'
    > print split(/.+/, $_, 0);
    >
    > $ perl -wle 'print split "+"'
    > Quantifier follows nothing in regex; marked by <-- HERE in m/+ <-- HERE
    > / at -e line 1.
    >
    > $ perl -wle 'print split /+/'
    > Quantifier follows nothing in regex; marked by <-- HERE in m/+ <-- HERE
    > / at -e line 1.
    >
    > $ perl -wle 'print split qr/+/'
    > Quantifier follows nothing in regex; marked by <-- HERE in m/+ <-- HERE
    > / at -e line 1.


    Great, that's informative. Thanks Doc.
     
    it_says_BALLS_on_your forehead, Jul 17, 2006
    #5
  6. it_says_BALLS_on_your forehead wrote:

    > I thought that qr was mainly used for pre-compiling variables into
    > regex patterns, but a colleague uses it like so:
    >
    > my ( $name, $value ) = split qr/=/, $string;
    >
    > Are there any benefits to doing this?


    It could be argued that it aids clarity.

    The built-in split() function has a magic prototype such that
    split(/=/, $string) is, in effect, treated as if you'd said
    split(qr/=/, $string). If you wanted to write your own split()-like
    function you'd need a qr// in the call.

    That said, I think most Perl programmers have gotten used to seeing
    split(/=/, $string) and would find the explicit qr// less clear.
     
    Brian McCauley, Jul 18, 2006
    #6
    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. Eddy C

    How would you use qsort to sort on a string

    Eddy C, Feb 9, 2006, in forum: C Programming
    Replies:
    31
    Views:
    1,791
    Jordan Abel
    Feb 13, 2006
  2. Replies:
    0
    Views:
    1,136
  3. Alexander
    Replies:
    620
    Views:
    8,288
    Seebs
    Nov 9, 2010
  4. Alexander
    Replies:
    68
    Views:
    1,528
  5. Anonieko Ramos

    What's wrong with rpc-literal? Why use doc-literal?

    Anonieko Ramos, Sep 27, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    381
    Anonieko Ramos
    Sep 27, 2004
Loading...

Share This Page