Hi, Monks: I used to use c++, but beginner for perl. I want to create a singleten class with a bunch of attributes and methods but didn't know how.

use strict; use warnings; package MyConfig; use XML::Simple; my $singleton; my $XMLfile_name=""; my @hostList = (); sub new { my $class = shift; #create singleton $singleton ||= bless {}, $class; #Error here: Useless use of hash element in void context $singleton->{$XMLfile_name}=$_[0]; return $singleton; } sub loadConfig{ my $self=shift; my $xml = XML::Simple->new; my $config = $xml->XMLin($file_name, ForceArray => [ 'host', 'component' ]) or die$!; my $index=0; while($config->{host}->{component}->[$index++]) { push @hostList, $config->{host}->{component}->[0]->{component_name}; } } 1;

The XML structure is like:

<root> <component component_name="n1" /> <component component_name="n2" /> <component component_name="n3" /> </root>

I call code as:

use strict; use MyConfig; my $CONFIG = "somefile.xml"; &main; sub main { my configReader = MyConfig->new( $CONFIG ); configReader->loadConfig; }

As you see, I didn't know how to create and use what we say member variable in c++. The $XMLfile_name and the @hostList here is using as member variables. @hostList used for holding results parsed from the XML file.

Another question: In the new method, as I read package method always put the invoker as the first argument. I suppose 'invoker' for new() means the class MyConfig and return an instance after blessed; And the 'invoker' for loadConfig seems change to be an instance, seems the $self by te first default argument is an instance not the class any more. But as I know the 'new' is an ordinary method as others, not a keywords, why they behaves different?

The last question:I don't think I made my while condition correct. How can I deal with the while index simply using $_? Thanks a lot in advance!!


In reply to how to make a package variable as c++ by anaconda_wly

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.