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

Dear Monks,

I am having problems deciphering this code:

my $perl5patt = qq{(??{join q{|}, $quotemeta \@{$var_name}})};

1) I can't figure out how this:

join q{|}, $quotemeta \@{$var_name}

fits the definition of join():

join EXPR,LIST

What is the list?

2) What is the '??' ?

It appears to be a function, but a search for a ?? function yields nothing.

Replies are listed 'Best First'.
Re: what does this code do?
by Corion (Patriarch) on Dec 26, 2009 at 21:01 UTC

    Have you looked at what the code returns?

    It merely quotes a string. It could have been written as

    my $perl5patt = "(??{join q{|}, $quotemeta \@{$var_name}})";

    I don't know where that string will be used, but it looks like it will be used in a regular expression and will try to match one or more words that are stored in the variable named by $var_name.

    Personally I think that it should be faster to (re)build the regular expression on each change of @$var_name, but I don't know what code you have that necessitates executing code when trying to match something. The documentation for the ??{} construct lives in perlre

Re: what does this code do?
by ikegami (Patriarch) on Dec 26, 2009 at 22:45 UTC

    There's no call to join in that call. You have a string constructor and an assignment.

    Presuming from the (??{ }), the string will later be interpolated into a regex pattern, at which point a match against that pattern could call join, but it won't be the join you descrived. It will be

    join q{|}, XXX \@{YYY}

    where XXX is the contents of $quotemeta and YYY is contents of $var_name joined by spaces. Double quoted strings (qq{}) interpolate.

    my $quotemeta = 'XXX'; my $var_name = 'YYY'; my $perl5patt = qq{(??{join q{|}, $quotemeta \@{$var_name}})}; print $perl5patt, "\n";
    (??{join q{|}, XXX @{YYY}})

    Since you didn't specify what $quotemeta and $var_name contain, we can't tell if the code executed by the regex engine will match join EXPR, LIST or not.

Re: what does this code do?
by Anonymous Monk on Dec 26, 2009 at 21:03 UTC
    qq{} s the same as "", its a string not code