Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Why do you think that that construct is illegal? I tried this code in perl 5.8.7 and it works without any warnings.

use strict; use warnings; use Data::Dumper; my $a; %$a=(a=>1,b=>2); print Dumper($a);

And it prints

$VAR1 = { 'a' => 1, 'b' => 2 };

UPDATE : I see now that the illegal part was the "my %$h" part rather than the assigment part. So that could have been changed simply by splitting the declaration to a separate line, or by changing the list to a {}.

However it seems an odd way to do it when you could just say :

$a={a=>1,b=>2};

I benchmarked the two versions, and the first %$a=() is 50% slower than the second ($a={}).

Regarding the difference between $h{name} versus $h->{name}, there is a slight speed difference (10%), because the second version first needs to dereference $h before doing the lookup, but it is so blazingly fast that I wouldn't worry about it. (Tested on a small hash - not sure for bigger hashes)

Rather just use whatever is easier to read, as there are bound to be other bottlenecks that make much more difference to your code's performance.


In reply to Re: Relative Merits of References by clinton
in thread Relative Merits of References by pbeckingham

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 romping around the Monastery: (4)
As of 2024-04-19 01:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found