List Info

Thread: Perl Software Developer and Database programmer




Perl Software Developer and Database programmer
user name
2006-02-22 12:36:33
On Wed, Feb 22, 2006 at 12:19:09PM +0000, Andy Armstrong
wrote:
> On 22 Feb 2006, at 12:07, Lusercop wrote:
>> this thread. (and given that you can simulate the
effects of local with a
>> more explicit set of mys, in a much clearer way,
then this is also true).
> What simulation of local are you advocating and why?

{
   my $newdollarslash = "whatIwant";
   my $savdollarslash = $/;
   $/ = $newdollarslash;

   dostuffwithappropriatecalls();

   $/ = $savdollarslash;
}

obviously.

this is the equivalent of:

{
   local $/;

   my $newdollarslash = "whatIwant";
   $/ = $newdollarslash;

   dostuffwithappropriatecalls();
}

Why? because it's clear that you're saving the value and
restoring it. For
those in the thread that didn't know that this was what
local did, that's
what it does. If the scope is quite long (then you should be
splitting it
into separate functions ...) then you are explicitly
restoring the value
back. I prefer this, stylewise.

-- 
Lusercop.net - LARTing Lusers everywhere since 2002
Perl Software Developer and Database programmer
user name
2006-02-22 12:47:05
On 22 Feb 2006, at 12:36, Lusercop wrote:
> obviously.

That's what I assumed you meant.

> Why? because it's clear that you're saving the value
and restoring  
> it. For
> those in the thread that didn't know that this was
what local did,  
> that's
> what it does. If the scope is quite long (then you
should be  
> splitting it
> into separate functions ...) then you are explicitly
restoring the  
> value
> back. I prefer this, stylewise.

Would you prefer explicit memory management too? In what way
is an  
explicit save / restore preferable to local (which is for
these  
purposes syntactic sugar on the same thing)? Is local really
so obscure?

-- 
Andy Armstrong, hexten.net

Perl Software Developer and Database programmer
user name
2006-02-22 13:10:54
On Wed, Feb 22, 2006 at 12:36:33PM +0000, Lusercop wrote:
> On Wed, Feb 22, 2006 at 12:19:09PM +0000, Andy
Armstrong wrote:
> > On 22 Feb 2006, at 12:07, Lusercop wrote:
> >> this thread. (and given that you can simulate
the effects of local with a
> >> more explicit set of mys, in a much clearer
way, then this is also true).
> > What simulation of local are you advocating and
why?
> 
> {
>    my $newdollarslash = "whatIwant";
>    my $savdollarslash = $/;
>    $/ = $newdollarslash;
> 
>    dostuffwithappropriatecalls();
> 
>    $/ = $savdollarslash;
> }
> 
> obviously.
> 
> this is the equivalent of:
> 
> {
>    local $/;
> 
>    my $newdollarslash = "whatIwant";
>    $/ = $newdollarslash;
> 
>    dostuffwithappropriatecalls();
> }
> 
> Why? because it's clear that you're saving the value
and restoring it. For
> those in the thread that didn't know that this was
what local did, that's
> what it does. If the scope is quite long (then you
should be splitting it
> into separate functions ...) then you are explicitly
restoring the value
> back. I prefer this, stylewise.

What about eval{}?  If you throw an exception, the old value
won't be
restored.

-Dom
Perl Software Developer and Database programmer
user name
2006-02-22 13:12:36
On Wednesday 22 February 2006 14:36, Lusercop wrote:
> On Wed, Feb 22, 2006 at 12:19:09PM +0000, Andy
Armstrong wrote:
>
> > What simulation of local are you advocating and
why?
>
> {
>    my $newdollarslash = "whatIwant";
>    my $savdollarslash = $/;
>    $/ = $newdollarslash;
>
>    dostuffwithappropriatecalls();
>
>    $/ = $savdollarslash;
> }

And you have to remember to set it back at the end. Why not
let perl remember 
for you?

{
    local $/ = "whatIwant";

    dostuffwithappropriatecalls();
}  # everything is back as before

If you want to localise a value, don't use a ``global''
assignment and make 
the maintenance programmer search for the other half of your
intention: use 
the facility the language provides, and put the information
in one place.

my localises a name in space; local localises a value in
time. There are two 
keywords because these are two different things.

Jonathan
Perl Software Developer and Database programmer
user name
2006-02-22 13:23:26
On Wed, Feb 22, 2006 at 03:12:36PM +0200, Jonathan McKeown
wrote:

Wow, you still exist! When are you coming back to bond?

> If you want to localise a value, don't use a
``global'' assignment and make 
> the maintenance programmer search for the other half of
your intention: use 
> the facility the language provides, and put the
information in one place.

I don't like the use of the magic variables either in
production code.

If it's not a magic variable, the question is more, what is
this global
variable doing in the first place, that it needs localising?

> my localises a name in space; local localises a value
in time. There are two 
> keywords because these are two different things.

I understand the difference, thanks. 

My point stands: how often have you used it in production
code, apart from
the pattern of local $/, which I don't like for other
reasons (mainly
inefficiency on reads)? And, of course, if you are using it,
hopefully it's
tightly coupled to a very reusable module rather than
scattered all over the
code.

-- 
Lusercop.net - LARTing Lusers everywhere since 2002
Perl Software Developer and Database programmer
user name
2006-02-22 14:06:25
On Wednesday 22 February 2006 15:23, Lusercop wrote:

> Wow, you still exist! When are you coming back to bond?

Back for a Christmas visit this year. Since Christmas is the
long summer 
holiday here, I'm hoping to be over for a while: certainly
long enough to 
attend (or possibly cause!) a social.

> I don't like the use of the magic variables either in
production code.
>
> If it's not a magic variable, the question is more,
what is this global
> variable doing in the first place, that it needs
localising?

True - but as long as magic variables are global, you do
need local, and you 
never know when else you might need it: one should probably
also know why one 
probably won't need it.

There's an old joke about a rockstar going to buy a
Rolls-Royce: he queries 
the presence of a toolkit in the boot, saying, "I
thought you told me this 
thing was reliable". The salesman's response:
"Do you have nipples, Sir?"
"Yes."
"And are you ever likely to breast-feed?"
"Don't be stupid."
"Quite, Sir: but they *are* there - Just In
Case."

local is there Just In Case.

Jonathan
Perl Software Developer and Database programmer
user name
2006-02-23 17:32:21
On Wed, Feb 22, 2006 at 12:36:33PM +0000, Lusercop wrote:

> {
>    my $newdollarslash = "whatIwant";
>    my $savdollarslash = $/;
>    $/ = $newdollarslash;
>    dostuffwithappropriatecalls();
>    $/ = $savdollarslash;
> }
> 
> this is the equivalent of:
> 
> {
>    local $/;
>    my $newdollarslash = "whatIwant";
>    $/ = $newdollarslash;
>    dostuffwithappropriatecalls();
> }
> 
> Why? because it's clear that you're saving the value
and restoring it.

Using a comment, however, is even clearer ...

{
    local $/ = 'foo';  # temporarily change $/ until we
hit end of block
    dostuff()
}

-- 
David Cantrell | http://www.cantrell.
org.uk/david

EIN KIRCHE! EIN KREDO! EIN PAPST!
[1-7]

about | contact  Other archives ( Real Estate discussion Medical topics )