Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: perl statements

by nefigah (Monk)
on Mar 25, 2008 at 04:32 UTC ( #676046=note: print w/ replies, xml ) Need Help??


in reply to perl statements

A good rule of thumb to go by, is that if something looks kinda funky and complicated and hard to understand, it probably is :P If you have Perl Best Practices, it will certainly pound that into your head. The semantics of the examples were explained above; just know that you won't need to use them yourself much or really worry about it.

Things to look out for:

  • C does have the comma usage as well, seen sometimes in things like:
    for (i = 0, j = 10; i < 10; i++, j--) { . . .}
    The commas there, like in Perl, do separate statements.
  • Note (as was well stated in the previous reply) that you can't do this, the likes of which you CAN do in C:
    my $var1, $var2 = 3, 4;
    As in your example, commas aren't making lists! (If you are running with use strict; like you always should, it will catch this for sure.) What you probably wanted was:
    my ($var1, $var2) = (3, 4); which assigns 3 to $var1 and 4 to $var2.
  • The block construct shown in the second example can be useful for creating a scope (that is, giving you more exact control about the breadth of your actions within it). An example would be:
    my $code = do { local $/; <$in> };
    (Stolen from PBP, which states: Note that it's important to put that localization-and-read inside a do {...} or in some other small block. A common mistake is to write this instead:

    $/ = undef; my $text = <$in>;

    That works perfectly well, in itself, but it also undefines the global input record separator, rather than its temporary localized replacement. But the global input record separator controls the read behaviour of every filehandleeven those that are lexically scoped, or in other packages. So, if you don't localize the change in $/ to some small scope, you're dooming every subsequent read everywhere in your program to vile slurpitude.

In summary, the examples listed are fairly rare, but like everything occasionally have application. Good luck and happy coding :)


I'm a peripheral visionary... I can see into the future, but just way off to the side.


Comment on Re: perl statements
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2014-10-23 22:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (129 votes), past polls