Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

stuck with a \N{CHAR NAME} problem

by amir_e_a (Hermit)
on May 26, 2011 at 13:59 UTC ( #906834=perlquestion: print w/replies, xml ) Need Help??

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

I am running this code on 5.14:

use 5.014; use charnames ':full'; my $cgj = '\N{COMBINING GRAPHEME JOINER}'; say $cgj; my $cgj_re = qr/$cgj/xms;

And i get the error: "\N{NAME} must be resolved by the lexer in regex; marked by <-- HERE in m/\N{COMBINING GRAPHEME JOINER <-- HERE }/ at lexer.pl line 6."

Does anyone have any idea how to make it work?

(A bit of background: The code above is a minimal problem. In my actual program i read a string from a file and turn it into a regex.)

Replies are listed 'Best First'.
Re: stuck with a \N{CHAR NAME} problem
by wind (Priest) on May 26, 2011 at 15:13 UTC

    You need to use double quotes to interpolate the character in $cgj:

    my $cgj = "\N{COMBINING GRAPHEME JOINER}"; my $cgj_re = qr/$cgj/xms;

    or just include it directly in the cached regex.

    my $cgj_re = qr/\N{COMBINING GRAPHEME JOINER}/xms;
Re: stuck with a \N{CHAR NAME} problem
by mje (Curate) on May 26, 2011 at 14:24 UTC

    Shouldn't that be double quotes not single quotes around the \N.

Re: stuck with a \N{CHAR NAME} problem
by ikegami (Pope) on May 26, 2011 at 16:02 UTC
    { use charnames ":alias" => { FOO => 0x2660 }; my $pat1 = '\N{FOO}'; $re1 = qr/$pat1/; } { use charnames ":alias" => { FOO => 0x2661 }; my $pat2 = '\N{FOO}'; $re2 = qr/$pat2/; } { use charnames ":alias" => { FOO => 0x2662 }; /$re1$re2/ }

    What should the pattern match? Probably /\N{U+2660}\N{U+2661}/, but the information is not available to qr// at run-time, much less to the m// operator that's not in scope of the relevant directives.

    Those problems were resolved by converting \N{NAME} to \N{U+NUM} at compile-time, and throwing an error when presented with \N{NAME} at run-time.

    "\N{NAME}" works. qr/\N{NAME}/ works. Others, not so much.

      Alias games aside, I have yet to read any argument for why \N{NAME} should not simply use the currently active charmap, and/or autoload the :full one. This would handle all but the rarest sitatuations, and it would make Perl stop violating the principle of least surprise. In other words, instead of generating an exception like
      Constant(\N{NAME}) unknown: (possibly a missing "use charnames ...")
      It should in that situation implicitly do a
      use charnames ":full";
      and get on with it. In other words, if you need a charmap and don't have one, just load what everybody is expecting.

        Auto-loading :full would be most desireable, but it wouldn't help here.

        It seems to me the OP's issue could be resolved by attaching charnames data to statements (just like strict flags). Then, qr// could translate \N{NAME} to \N{U+NUM} at run-time just like it does at compile-time. That would even handle aliases. Is that hard to do?

Re: stuck with a \N{CHAR NAME} problem
by (anonymized user) (Curate) on May 26, 2011 at 14:15 UTC
    Hmmm short answer is that everything is working perfectly - Perl is supposed to give an error if the regexp is incorrect ;) - Seriously though, what functionality are you actually wanting?

    One world, one people

      Well, *I* would want Perl to accept this. Either by just DWIM (heh, if it can figure out what is wanted when giving the error message, why not just f*cking do it? If I wanted Pythonesque behaviour, I would have run python), or by just not doing anything special.

      Preferably the former.

      IMO, it's ok for a language to say "look buddy, I do not know what you mean here". But languages that say "oh, I know exactly what you mean, but I'm not going to do it because you didn't use some magic incantation" have no business to exists outside fora like the hates-software mailing list.

        Is there another language really that DWIM? You know, I could do with a good reason to switch to part-time work ;)

        One world, one people

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://906834]
Approved by rovf
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2019-12-10 13:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?