Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

regexp "?" delimiter

by cmic (Acolyte)
on Jun 11, 2019 at 13:54 UTC ( [id://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 ./regexp.pl 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 (Cardinal) 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 (Patriarch) 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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (10)
As of 2024-04-18 09:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found