Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Re: Tact and the Monastery

by talexb (Canon)
on Sep 20, 2002 at 17:01 UTC ( #199529=note: print w/ replies, xml ) Need Help??


in reply to Re: Tact and the Monastery
in thread Tact and the Monastery

I would probably change that to look like

# crypt the password my $crypted; { my @chars=(a..z,A..Z,0..9,'.','/'); my $salt= $chars[rand(@chars)] . $chars[rand(@chars)]; $crypted=crypt($password,$salt); }
so that the temporary variables (@chars and $salt) disappear after the closure ends. And I agree with the suggestion lower down: you could lose $salt and incorporate the salting in the call to crypt.

--t. alex
but my friends call me T.


Comment on Re: Re: Tact and the Monastery
Select or Download Code
Re^3: Tact and the Monastery
by Aristotle (Chancellor) on Sep 21, 2002 at 08:55 UTC
    That's not a closure. :-) It's just a naked block.

    Makeshifts last the longest.

      I have to disagree: because I have declared a variable inside that block, it becomes a closure. When the closure ends, the variables cease to exist.

      This is a Good Idea because it means the variables a) aren't sitting around waiting to Mess Things Up later on when the next piece of code uses variables with the same name and b) aren't using up memory that's no longer needed.

      Hence, my downvote.

      --t. alex
      but my friends call me T.

      Update:And sometimes I'm an idiot.

      You're absolutely right, that's not a closure. I was getting closure and scope mixed up. A scoped variable does disappear when it goes out of scope. A variable defined within a closure continues to exist for as long as the closure exists.

      My apologies.

        I --ed you in turn.
        my $closure; { my $variable; $closure = sub { $variable = shift }; }

        The inner block is now a closure. The $variable will NOT disappear at the end of the scope wherein it was defined. The outer naked block is just a naked block. A closure can only be dynamically constructed and is a block that preserves a variable beyond the scope it was defined in.

        Your own example shows just a naked block. You have perfectly explained its virtues which I was already aware (and an advocate) of, and which I did not challenge in the first place. But there is no closure in your code. Please check your definitions.

        Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (16)
As of 2014-04-16 14:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (430 votes), past polls