What is precedence in this expression?
When I run this program:
print(rand*100)
I get values from [0,1)
range.
But for this:
print(100*rand)
I get values from [0,100)
range.
What is precedence here? and why first expression does not return values from [0,100)
range?
perl perlop
add a comment |
When I run this program:
print(rand*100)
I get values from [0,1)
range.
But for this:
print(100*rand)
I get values from [0,100)
range.
What is precedence here? and why first expression does not return values from [0,100)
range?
perl perlop
add a comment |
When I run this program:
print(rand*100)
I get values from [0,1)
range.
But for this:
print(100*rand)
I get values from [0,100)
range.
What is precedence here? and why first expression does not return values from [0,100)
range?
perl perlop
When I run this program:
print(rand*100)
I get values from [0,1)
range.
But for this:
print(100*rand)
I get values from [0,100)
range.
What is precedence here? and why first expression does not return values from [0,100)
range?
perl perlop
perl perlop
asked Nov 25 '18 at 14:59
Eugen KonkovEugen Konkov
5,89533861
5,89533861
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
rand
has two syntax:
rand
rand EXPR
If what follows rand
can be the start of an expression (EXPR
), Perl assumes you are using the latter form.
*
can start an EXPR
, so rand*...
is parsed as rand EXPR
. This means that rand*100
is equivalent to rand(*100)
.
$ perl -MO=Deparse,-p -wle'print(rand*100)'
BEGIN { $^W = 1; }
BEGIN { $/ = "n"; $ = "n"; }
print(rand(*100));
-e syntax OK
$ perl -wle'print(rand*100)'
Argument "*main::100" isn't numeric in rand at -e line 1.
0.57355563536203
Two things that make this ambiguity even more fun: Spaces between sigils and variable names are allowed, sorand * 100
still does not avoid this issue; and it happens withrand / 100
too just resulting in a parse error once the parser realizes there's no closing/
.
– Grinnz
Nov 25 '18 at 17:31
add a comment |
You can always use B::Deparse to see how Perl is parsing an expression.
$ perl -MO=Deparse -e'print(100*rand)'
print 100 * (rand);
-e syntax OK
$ perl -MO=Deparse -e'print(rand*100)'
print rand *100;
-e syntax OK
What an honour ... I've actually got your "Data Munging with Perl" on my bookshelf directly above me. Metaphoric in more ways than one ;-)
– Michael G.
Nov 25 '18 at 15:36
1
-MO=Deparse,-p
makes it clearer still.
– mob
Nov 25 '18 at 16:55
add a comment |
The problem is not about presidence, it is about syntax. The presence of the glob "*", which renders the number unworkable.
print rand 100;
= 0 - 100
print (rand(100));
... gives the same result.
print rand *100
... is ignored
Essentially you have created a number "*100". "i100" is an interesting number however, but I'm not sure how it is randomised :-)
1
The number is not*100
but the numification of the variable*100
, which is at that point an undefined glob.
– Grinnz
Nov 25 '18 at 17:34
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53468764%2fwhat-is-precedence-in-this-expression%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
rand
has two syntax:
rand
rand EXPR
If what follows rand
can be the start of an expression (EXPR
), Perl assumes you are using the latter form.
*
can start an EXPR
, so rand*...
is parsed as rand EXPR
. This means that rand*100
is equivalent to rand(*100)
.
$ perl -MO=Deparse,-p -wle'print(rand*100)'
BEGIN { $^W = 1; }
BEGIN { $/ = "n"; $ = "n"; }
print(rand(*100));
-e syntax OK
$ perl -wle'print(rand*100)'
Argument "*main::100" isn't numeric in rand at -e line 1.
0.57355563536203
Two things that make this ambiguity even more fun: Spaces between sigils and variable names are allowed, sorand * 100
still does not avoid this issue; and it happens withrand / 100
too just resulting in a parse error once the parser realizes there's no closing/
.
– Grinnz
Nov 25 '18 at 17:31
add a comment |
rand
has two syntax:
rand
rand EXPR
If what follows rand
can be the start of an expression (EXPR
), Perl assumes you are using the latter form.
*
can start an EXPR
, so rand*...
is parsed as rand EXPR
. This means that rand*100
is equivalent to rand(*100)
.
$ perl -MO=Deparse,-p -wle'print(rand*100)'
BEGIN { $^W = 1; }
BEGIN { $/ = "n"; $ = "n"; }
print(rand(*100));
-e syntax OK
$ perl -wle'print(rand*100)'
Argument "*main::100" isn't numeric in rand at -e line 1.
0.57355563536203
Two things that make this ambiguity even more fun: Spaces between sigils and variable names are allowed, sorand * 100
still does not avoid this issue; and it happens withrand / 100
too just resulting in a parse error once the parser realizes there's no closing/
.
– Grinnz
Nov 25 '18 at 17:31
add a comment |
rand
has two syntax:
rand
rand EXPR
If what follows rand
can be the start of an expression (EXPR
), Perl assumes you are using the latter form.
*
can start an EXPR
, so rand*...
is parsed as rand EXPR
. This means that rand*100
is equivalent to rand(*100)
.
$ perl -MO=Deparse,-p -wle'print(rand*100)'
BEGIN { $^W = 1; }
BEGIN { $/ = "n"; $ = "n"; }
print(rand(*100));
-e syntax OK
$ perl -wle'print(rand*100)'
Argument "*main::100" isn't numeric in rand at -e line 1.
0.57355563536203
rand
has two syntax:
rand
rand EXPR
If what follows rand
can be the start of an expression (EXPR
), Perl assumes you are using the latter form.
*
can start an EXPR
, so rand*...
is parsed as rand EXPR
. This means that rand*100
is equivalent to rand(*100)
.
$ perl -MO=Deparse,-p -wle'print(rand*100)'
BEGIN { $^W = 1; }
BEGIN { $/ = "n"; $ = "n"; }
print(rand(*100));
-e syntax OK
$ perl -wle'print(rand*100)'
Argument "*main::100" isn't numeric in rand at -e line 1.
0.57355563536203
edited Nov 26 '18 at 2:00
answered Nov 25 '18 at 15:09
ikegamiikegami
263k11177397
263k11177397
Two things that make this ambiguity even more fun: Spaces between sigils and variable names are allowed, sorand * 100
still does not avoid this issue; and it happens withrand / 100
too just resulting in a parse error once the parser realizes there's no closing/
.
– Grinnz
Nov 25 '18 at 17:31
add a comment |
Two things that make this ambiguity even more fun: Spaces between sigils and variable names are allowed, sorand * 100
still does not avoid this issue; and it happens withrand / 100
too just resulting in a parse error once the parser realizes there's no closing/
.
– Grinnz
Nov 25 '18 at 17:31
Two things that make this ambiguity even more fun: Spaces between sigils and variable names are allowed, so
rand * 100
still does not avoid this issue; and it happens with rand / 100
too just resulting in a parse error once the parser realizes there's no closing /
.– Grinnz
Nov 25 '18 at 17:31
Two things that make this ambiguity even more fun: Spaces between sigils and variable names are allowed, so
rand * 100
still does not avoid this issue; and it happens with rand / 100
too just resulting in a parse error once the parser realizes there's no closing /
.– Grinnz
Nov 25 '18 at 17:31
add a comment |
You can always use B::Deparse to see how Perl is parsing an expression.
$ perl -MO=Deparse -e'print(100*rand)'
print 100 * (rand);
-e syntax OK
$ perl -MO=Deparse -e'print(rand*100)'
print rand *100;
-e syntax OK
What an honour ... I've actually got your "Data Munging with Perl" on my bookshelf directly above me. Metaphoric in more ways than one ;-)
– Michael G.
Nov 25 '18 at 15:36
1
-MO=Deparse,-p
makes it clearer still.
– mob
Nov 25 '18 at 16:55
add a comment |
You can always use B::Deparse to see how Perl is parsing an expression.
$ perl -MO=Deparse -e'print(100*rand)'
print 100 * (rand);
-e syntax OK
$ perl -MO=Deparse -e'print(rand*100)'
print rand *100;
-e syntax OK
What an honour ... I've actually got your "Data Munging with Perl" on my bookshelf directly above me. Metaphoric in more ways than one ;-)
– Michael G.
Nov 25 '18 at 15:36
1
-MO=Deparse,-p
makes it clearer still.
– mob
Nov 25 '18 at 16:55
add a comment |
You can always use B::Deparse to see how Perl is parsing an expression.
$ perl -MO=Deparse -e'print(100*rand)'
print 100 * (rand);
-e syntax OK
$ perl -MO=Deparse -e'print(rand*100)'
print rand *100;
-e syntax OK
You can always use B::Deparse to see how Perl is parsing an expression.
$ perl -MO=Deparse -e'print(100*rand)'
print 100 * (rand);
-e syntax OK
$ perl -MO=Deparse -e'print(rand*100)'
print rand *100;
-e syntax OK
answered Nov 25 '18 at 15:12
Dave CrossDave Cross
47.3k33978
47.3k33978
What an honour ... I've actually got your "Data Munging with Perl" on my bookshelf directly above me. Metaphoric in more ways than one ;-)
– Michael G.
Nov 25 '18 at 15:36
1
-MO=Deparse,-p
makes it clearer still.
– mob
Nov 25 '18 at 16:55
add a comment |
What an honour ... I've actually got your "Data Munging with Perl" on my bookshelf directly above me. Metaphoric in more ways than one ;-)
– Michael G.
Nov 25 '18 at 15:36
1
-MO=Deparse,-p
makes it clearer still.
– mob
Nov 25 '18 at 16:55
What an honour ... I've actually got your "Data Munging with Perl" on my bookshelf directly above me. Metaphoric in more ways than one ;-)
– Michael G.
Nov 25 '18 at 15:36
What an honour ... I've actually got your "Data Munging with Perl" on my bookshelf directly above me. Metaphoric in more ways than one ;-)
– Michael G.
Nov 25 '18 at 15:36
1
1
-MO=Deparse,-p
makes it clearer still.– mob
Nov 25 '18 at 16:55
-MO=Deparse,-p
makes it clearer still.– mob
Nov 25 '18 at 16:55
add a comment |
The problem is not about presidence, it is about syntax. The presence of the glob "*", which renders the number unworkable.
print rand 100;
= 0 - 100
print (rand(100));
... gives the same result.
print rand *100
... is ignored
Essentially you have created a number "*100". "i100" is an interesting number however, but I'm not sure how it is randomised :-)
1
The number is not*100
but the numification of the variable*100
, which is at that point an undefined glob.
– Grinnz
Nov 25 '18 at 17:34
add a comment |
The problem is not about presidence, it is about syntax. The presence of the glob "*", which renders the number unworkable.
print rand 100;
= 0 - 100
print (rand(100));
... gives the same result.
print rand *100
... is ignored
Essentially you have created a number "*100". "i100" is an interesting number however, but I'm not sure how it is randomised :-)
1
The number is not*100
but the numification of the variable*100
, which is at that point an undefined glob.
– Grinnz
Nov 25 '18 at 17:34
add a comment |
The problem is not about presidence, it is about syntax. The presence of the glob "*", which renders the number unworkable.
print rand 100;
= 0 - 100
print (rand(100));
... gives the same result.
print rand *100
... is ignored
Essentially you have created a number "*100". "i100" is an interesting number however, but I'm not sure how it is randomised :-)
The problem is not about presidence, it is about syntax. The presence of the glob "*", which renders the number unworkable.
print rand 100;
= 0 - 100
print (rand(100));
... gives the same result.
print rand *100
... is ignored
Essentially you have created a number "*100". "i100" is an interesting number however, but I'm not sure how it is randomised :-)
answered Nov 25 '18 at 15:18
Michael G.Michael G.
2221316
2221316
1
The number is not*100
but the numification of the variable*100
, which is at that point an undefined glob.
– Grinnz
Nov 25 '18 at 17:34
add a comment |
1
The number is not*100
but the numification of the variable*100
, which is at that point an undefined glob.
– Grinnz
Nov 25 '18 at 17:34
1
1
The number is not
*100
but the numification of the variable *100
, which is at that point an undefined glob.– Grinnz
Nov 25 '18 at 17:34
The number is not
*100
but the numification of the variable *100
, which is at that point an undefined glob.– Grinnz
Nov 25 '18 at 17:34
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53468764%2fwhat-is-precedence-in-this-expression%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown