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.

ihb

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.