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

Hi, I have problem comprehend what is it trying to do in the code tag below. Can anyone help?

our @proj=qw( A B C ); our $proj=test:query('project', '=','or', @proj);

sub query($$$@){ my($name, $type, $comb,@vals)=@_; @vals=map{ s/'/''; "[$name] $type '$_'";}@vals; return "(".join(" $comb",@vals). ")"; }

Replies are listed 'Best First'.
Re: I dont understand what it is doing....
by ikegami (Patriarch) on Jun 07, 2010 at 17:16 UTC

    Just from looking at it,

    It's giving a syntax error because "test:query" is missing a ":".

    It's giving a syntax error because the substitution isn't terminated. (s/'/'';s/'/''/g;???)

    It's giving a warning because the function has a prototype and a call to it was encountered before its declaration was encountered.

Re: I dont understand what it is doing....
by choroba (Cardinal) on Jun 07, 2010 at 17:18 UTC
    Well, if you add an ending slash to the substitution inside of map, the code will run, so you can try what it does. I had to remove the test: as well. To see the output, add a similar line:
    print $proj,"\n";
    However, if you turn warnings on, you will see that prototypes are defined after the sub has been called which means they are ignored.
Re: I dont understand what it is doing....
by biohisham (Priest) on Jun 08, 2010 at 04:30 UTC
    Regardless of the errors that hinder this code from working and which have been duly explained to you, the subroutine in here has a prototype checking mechanism in place, however, this mechanism is a compile-time mechanism, so to avail of it you got to declare your subroutine and then call it afterwards, if you do it the other way around, i.e you wrote the subroutine call before defining it the prototype checking would simply get ignored and the subroutine would get executed as long as it is free from other errors (logic, syntax, keyword misspelling...).

    What this code above is supposed to be doing? , well, the subroutine named "query" has been declared, then when you call it and simultaneously provide arguments, it goes checking if these arguments correspond to the prototype predeclared.

    After that each one of the arguments is read into the variables "$name, $type, $comb, @vals" and in the subsequent line @vals contents are modified via the map function and reassigned to @vals again.

    The subroutine returns a string of @vals elements enclosed within () and joined together with '=' sign.

    Welcome to the Monastery noobie82, familiarize yourself around and make a good use of the Perl documentations...


    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.
Re: I dont understand what it is doing....
by noobie82 (Initiate) on Jun 07, 2010 at 17:38 UTC
    Actually I just dont understand what does the below code trying to do :( can somebody explain to me?
      It's trying to create a string. As to what that strings looks like, fix the errors, run it and print the content of $proj and you'll see.

      The code doesn't compile, AND has no comments.

      Clearly, the solution here is to find the original author, and ask what the heck they were thinking when they committed it to the repository.

      I mean this part:
      sub query($$$@){ my($name, $type, $comb,@vals)=@_; @vals=map{ s/'/''; "[$name] $type '$_'";}@vals; return "(".join(" $comb",@vals). ")"; }
        It's not doing anything, except throwing an error. What is it supposed to do?