Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: Re: Re: How Perl Optimize your code & some code TIPS >=/

by ihb (Deacon)
on Jan 31, 2003 at 15:06 UTC ( #231634=note: print w/replies, xml ) Need Help??

in reply to Re: Re: How Perl Optimize your code & some code TIPS >=/
in thread How Perl Optimize your code & some code TIPS ;-P

Doesn't need to use the my outside, because Perl only do the my 1 time (is always the same scalar reference)!

Actually, the my() is always done. What happens though is that if old $foo's refcount is zero then it's safe to reuse that slot, so it is reused. But my() is always executed. Afaik, it's the same for

while(1){ my $var = &foo() ; ... }
which you do advocate against. That my() has a run-time effect can easily be proven by the following one-liner:   perl -wle'my $last; while (my $line = <STDIN>) { last if $line =~ /^$/; print \$line; $last = \$line; }'
It would be insane if my() didn't create a new variable each time it's executed. Per idea, my() does that, but the reuse feature you have noticed is just an optimization.

This will load only the Key

No, first a list is created out of the expression keys %{$rf_h} which then is passed to foreach. So an unnecessary list is built. $Key isn't a copy of the element in the list, it's an alias for it. And here the my() works the same as described above.

Well, of course, this isn't a tut for reference! How I told, is only a tip!

... and I don't like the way you tipped. You did something between telling that the feature exists, and telling how to use them. You wrote so that people can cut-n-paste and use this without understanding it. I would have nothing against, in fact I'd like it, if there was something like "Another good thing to use when you have large variables is to pass them as references." and then give a couple of scenarios where references would've been a great benefit. And then a pointer to perlreftut so people can go and learn this wonderful feature.

Well, perhaps you don't know the difference of &foo and foo().

Heh. A couple of my previous posts pointing out exactly this issue: Re: Hash values and constants, Re: Re: Forward-referenceing subs, Re: Hash of Hash of Listed subroutines.

If you use &foo, this is set as a sub, and the arguments will be the previus @_, if defined, or the content of (...).

This is an ambiguous statement. There won't be any arguments, so the arguments won't be the previous @_. The @_ seen in the called subroutine is the same, I repeat, the same, as the caller's.

sub foo { print shift } sub bar { &foo; print @_ } # BAD!
Here the &foo; call will steal the first element in bar's @_.

The other issue with & is that it makes perl ignore prototypes.

But anyway, I was showing how if() evaluate the conditions, in this case the subs, not how to call a sub!

Now that's a lame excuse! Don't you think that demonstrative code should be sane? If I want to demonstrate how a keyboard works, I don't type upside down. I use it as it should be used. And I have it connected to my computer, not my toaster.

And if you don't know yet, the world is not perfect!

Yeah, you're right. Why do I bother? Let's just give it up! The world is such a bad place anyway... eh...

Not trying to "create" fool erros in my node!

And my reason to ""create" fool erros" would be...?

I liked the initiative, though.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://231634]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2018-04-25 09:39 GMT
Find Nodes?
    Voting Booth?