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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

As moritz said, this isn't OO, but the second line will work with one small modification:

print "$BldEnv{TTT2}->{VersionTwo}\n";

Your version didn't work because the first -> was trying to dereference $BldEnv as though it were a reference to a hash. But WAIT! $BldEnv doesn't exist! You declared %BldEnv. Different animal! Despite using the $ sigil to get at hash values, when you added the -> after it, Perl was looking for a SCALAR variable and ignoring the HASH that you defined!

Perl's syntactic sugar allows you to get around derefencing the 'TTT2' hash using ->. As such, the first one is better.

Further to this, you really don't need to do string interpolation... can get a little messy with multi-level data structures. You could just do:

print $BldEnv{TTT2}{VersionTwo},"\n";

That way the variable is easier to see and prevents a number of interpolation pitfalls. If you're feeling adventurous, you could install the Perl6::Say module so you don't have to append that "\n" to the end like so:

use Perl6::Say; say $BldEnv{TTT2}{VersionTwo};

The "\n" is auto-appended by say.

What you should _definately_ always do do is begin each script with:

use strict; use warnings;

This would have thrown an error on your last print statement telling you that you that $BldEnv hadn't been declared, cluing you in that you're doing something wrong.

Happy Perl-ing!

update: expanded on difference between $BldEnv{key} and $BldEnv->{key}.

update the second: fixed errorneous variable naming.

--
meraxes

In reply to Re: Using OO hash by meraxes
in thread Using OO hash by monk2b

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 chanting in the Monastery: (7)
As of 2024-04-19 06:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found