Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-24 10:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found