Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^3: When doesn't the flip-flop operator work in all scalar contexts?

by ikegami (Patriarch)
on Oct 02, 2008 at 19:31 UTC ( [id://715079]=note: print w/replies, xml ) Need Help??


in reply to Re^2: When doesn't the flip-flop operator work in all scalar contexts?
in thread Why doesn't the flip-flop operator work in all scalar contexts?

while (<$fh>) { next if 1 .. 1; print "$_\n"; }

means

while (<$fh>) { next if $.==1 .. $.==1; print "$_\n"; }

so to do the same with a loop which counts over something other than $., you have specify it explicitly.

for (1,2,3,4,5) { next if $_==1 .. $_==1; print "$_\n"; }

what do they mean by "constant expression?"

Short: Anything that Perl converts to a constant at compile-time.

>perl -MO=Concise -e"print( 4 + 1 * 2 )" 6 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 5 <@> print vK ->6 3 <0> pushmark s ->4 4 <$> const[IV 6] s ->5 <--- 4+1*2 -e syntax OK >perl -MO=Concise -e"print( 4 + $x * 2 )" a <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 9 <@> print vK ->a 3 <0> pushmark s ->4 8 <2> add[t3] sK/2 ->9 \ 4 <$> const[IV 4] s ->5 | 7 <2> multiply[t2] sK/2 ->8 | <--- 4+$x*2 - <1> ex-rv2sv sK/1 ->6 | 5 <#> gvsv[*x] s ->6 | 6 <$> const[IV 2] s ->7 /

Long: A constant expression is known to Perl to always return the same result. It includes constants (including those created by constant), most operators operating on constants, some builtin functions, and functions with the () prototype whose body consist entirely of a constant expression (as per Constant Functions in perlsub).

Given

sub PI() { 4 * atan2(1, 1) } use constant SOMENUM => rand(4);

Example of constant expressions:

4 5+6*7 4 * atan2(1, 1) PI SOMENUM

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-25 17:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found