Hi,
Hello,
I wrote the following:
#!/usr/bin/perl -wT
use strict;
my ($server) = sitemode("test"); print $server;
sub sitemode { my $string = "foonky"; my $server = shift || $string =~ /(foo|bar)/ ? $1 : 'default'; return $server; }
Now this throws an error. The error does not occur when I call the subroutine without an argument:
It is actually a warning, not an error. An error would exit the program without running it.
sitemode();
It also does not show up if I enclose the ternary operator in brackets:
my $server = shift || ($string =~ /(foo|bar)/ ? $1 : 'default');
While this solves my problem, I still do not get the reason for it. Could someone shed a light on this precedence confusion?
You can ask perl to do it for you:
$ perl -MO=Deparse,-p -e'sub sitemode { my $server = shift || $string =~ /(foo|bar)/ ? $1 : "default"; }'
sub sitemode {
(my $server = ((shift(@_) || ($string =~ /(foo|bar)/)) ? $1 : 'default'));
}
-e syntax OK
If we remove some parentheses:
my $server = ( shift || ( $string =~ /(foo|bar)/ ) ) ? $1 : 'default';
Or you could just read perlop:
perldoc perlop [snip] left =~ !~ left * / % x left + - . left << >> nonassoc named unary operators nonassoc < > <= >= lt gt le ge nonassoc == != <=> eq ne cmp left & left | ^ left && left || nonassoc .. ... right ?: right = += -= *= etc.
Where you can see that =~ has higher precedence than || which has higher precedence than ?: which has higher precedence than =.
John -- use Perl; program fulfillment
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>