Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^3: Programming in Perl without semicolon

by JavaFan (Canon)
on Jan 31, 2009 at 08:37 UTC ( [id://740395]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Programming in Perl without semicolon
in thread Programming in Perl without semicolon

Bonus: Doesn't confine you to the main namespace!
That's only a bonus if you can switch packages without a semicolon. I haven't figured out how, except for the useless:
{package Foo}
(local our $n = shift) && do { ... }
I'm surprised this works. I would have expected 'our' to be in effect after the statement, just like 'my'. (If you replace 'local our' with 'my', the code wouldn't compile under 'strict').

Unfortunally, it's not a general technique. It works here because 'fib(0)' is supposed to return '0'; the block isn't executed if the argument of the function is 0.

Replies are listed 'Best First'.
Re^4: Programming in Perl without semicolon
by ikegami (Patriarch) on Jan 31, 2009 at 18:54 UTC

    That's only a bonus if you can switch packages without a semicolon.

    Oh yeah. Well, no bonus, but it still makes it more readable.

    I'm surprised this works.

    Me too, but it makes sense. It allows for

    our $state = $next_states{$state};

    Unfortunally, it's not a general technique.

    Oops! Just replace the and op with a list op or replace the sassign op with an aassign op.

    (local our $n = shift), ...

    or

    (local (our $n) = shift) && ...

    The former is simpler. The latter has the advantage that it doesn't affect the return value in list context.

Re^4: Programming in Perl without semicolon
by JavaFan (Canon) on Jan 31, 2009 at 10:20 UTC
    Unfortunally, it's not a general technique. It works here because 'fib(0)' is supposed to return '0'; the block isn't executed if the argument of the function is 0.
    But it can be written as:
    local our ($n, $m) = (shift, 0)) && do { }
    and then it will always do the 'do'.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (9)
As of 2024-03-28 09:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found