Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Using join and ternary operator together

by id007 (Initiate)
on Sep 10, 2013 at 11:16 UTC ( [id://1053234]=perlquestion: print w/replies, xml ) Need Help??

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

The following line throws error, don't know why.

my $stmt = join " ", "select * from dbo.server" , ($orderby) ? ";","or +der by $orderby;" ;

Replies are listed 'Best First'.
Re: Using join and ternary operator together
by Corion (Patriarch) on Sep 10, 2013 at 11:17 UTC

    What error does it throw?

    Maybe you want to look at what the ternary operator does (in perlop), and how it is spelled?

Re: Using join and ternary operator together
by hdb (Monsignor) on Sep 10, 2013 at 11:27 UTC

    ... check your punctuation ...

Re: Using join and ternary operator together
by id007 (Initiate) on Sep 11, 2013 at 02:50 UTC
    Thanks all for the replies. My syntax and logic both were incorrect. The working code is this: my $stmt = join " ", "select * from dbo.server" , defined($orderby) ? "order by $orderby;" : ";" ;

      Just a pet peeve for the most part, but I prefer using parens even when they are not strictly needed just to improve clarity of intent. Seems to help me, anyway.

      my $stmt = join(" ", "select * from dbo.server" , (defined($orderby) ? + "order by $orderby;" : ";"));

      On a side note, I presume you have a space in front of the word "order" in your true clause. I.e. " order by $orderby;" or else you would wind up with SQL that read something like this: select * from dbo.serverorder by .... That would clearly be wrong.

        ... a space in front of the word "order" in your true clause.

        That space would be supplied by the  " " join string of the join function.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-03-29 12:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found