Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

regexp "?" delimiter

by cmic (Acolyte)
on Jun 11, 2019 at 13:54 UTC ( #11101240=perlquestion: print w/replies, xml ) Need Help??

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

The following code (using Perl 5.24 on Debian) yields this error:
syntax error at ./ line xx, near "if ?"
my ($DICT, $first, $last); open $DICT, "<", "/usr/share/dict/words"; while (<$DICT>) { #FIXME $first = $1 if ?(^neuro.*)?; # <- syntax error here $last = $1 if /(^neuro.*)/; } close $DICT; print "first : $first, last : $last \n"; print "-" x 10, "\n"; #
Is it a deprecated usage of the "?" delimiter ?
Or what ?
-- cmic. Life helps. Perl Too.

Replies are listed 'Best First'.
Re: regexp "?" delimiter
by choroba (Bishop) on Jun 11, 2019 at 14:04 UTC
    Please, edit your node and move the question up away from the signature part. Some monks have turned signature display off.

    The ?...? delimiter should be written m?...? starting with 5.22.

    I'd extend the original code with

    use Syntax::Construct qw{ ?? };

    if it were still running in the original environment. But adding the m would work in the old versions, too, so it's an unnecessary step.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: regexp "?" delimiter
by Corion (Pope) on Jun 11, 2019 at 14:05 UTC

    The support for ?PATTERN? matching was removed in Perl 5.22.0, four years ago. You will need to use a matching operator and operand, like $_ =~ ?(^neuro.*)?. You will need to use a prefixed m, as choroba and Eily show.

    In the perldelta, you can read what changed between each version of Perl 5.

Re: regexp "?" delimiter
by Eily (Monsignor) on Jun 11, 2019 at 14:03 UTC

    To use a character as a delimiter, you have to prepend the corresponding operator to indicate what it is the delimiter of, q for a single quote string, y for a transliteration, m for a matching operation (regex)... The trick is, when perl sees / in a place where it could expect a regex, is guesses that you meant m/. So /(^neuro.*)/ is actually m/(^neuro.*)/ where m means "make / the delimiter for a match operation".

    So you should use m?(^neuro.*)?

    Edit: oh I thought the m had always been required except for //, turns out there used to be an exception for ?? as well.

Re: regexp "?" delimiter
by cmic (Acolyte) on Jun 12, 2019 at 13:14 UTC
    Thanks Eily for clear explanations.
    -- cmic. Retired Sysadmin....

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11101240]
Approved by Corion
Front-paged by haukex
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (9)
As of 2019-06-25 19:36 GMT
Find Nodes?
    Voting Booth?
    Is there a future for codeless software?

    Results (107 votes). Check out past polls.