Thank you very much Sam and Wade for the response. strict refs mode is fine. But I just wanted to know if I understood right! When you use the -> operator the '$' sigil is not necessary! Perl automatically understands the type i.e whether array or hash. And also the variable is defined in the current package. Is that correct?
I actually ran into problem while just trying to auto-generate getter/setters for a class. So just before installing the methods into the target class, the strict 'refs' is turned off! So the getter/setter code was something like this
sub {
if ( @_ == 1 ) {
$_[0]->{$name} # this was initially $_[0]{$name}
} else {
$_[0]->{$name} == $_[1]; # initially $_[0]{$name} = $_[1];
}
}
This seems to be ok as long as the method is called on an object. But say you call the method on a class(say by mistake), then it was causing problems without the arrow operator. But when the arrow operator was included it was working fine although the method was meant to be an object method. So then I saw I needed to brush up some fundametals!
So if I understand it correctly what happens here is that the hash variable is actually defined in the package where the code belongs. And it happens because strict refs is off.
Thanks in advance for the help.
Thanks again!
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.