Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Perl "new" command

by akagrawal3 (Beadle)
on Mar 01, 2012 at 06:29 UTC ( #957109=perlquestion: print w/ replies, xml ) Need Help??
akagrawal3 has asked for the wisdom of the Perl Monks concerning the following question:

Could somebody explain me the following piece of code. Are the keys mentioned predefined? What does the following code do?
StatView::Script->new({ 44 name => 'stv_copy_key', 45 version => $VERSION, 46 opt_spec => [qw( 47 host=s 48 list_pollers 49 all_pollers 50 )], 51 db_connection => [qw/ poller /], 52 handler => sub { StatView::CopySSHKey::run (@_) }, 53 });

Comment on Perl "new" command
Download Code
Re: Perl "new" command
by BrowserUk (Pope) on Mar 01, 2012 at 06:38 UTC
    What does the following code do?

    This StatView::Script->new( ... ); calls the new method of the class.

    The bit inside the parens:

    { name => 'stv_copy_key', version => $VERSION, opt_spec => [qw( host=s list_pollers all_pollers )], db_connection => [qw/ poller /], handler => sub { StatView::CopySSHKey::run (@_) }, }

    Constructs an anonymous hash which is passed into the new method.

    It is equivalent to doing

    my %hash = ( name => 'stv_copy_key', version => $VERSION, opt_spec => [qw( host=s list_pollers all_pollers )], db_connection => [qw/ poller /], handler => sub { StatView::CopySSHKey::run (@_) }, ); StatView::Script->new( \%hash );
    Are the keys mentioned predefined?

    Presumably you mean the hash keys? If so, one assumes that they have some meaning to the class constructor, otherwise they would either raise an error or be ignored.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

Re: Perl "new" command
by tobyink (Abbot) on Mar 01, 2012 at 07:15 UTC

    This:

    Foo->bar($baz)

    is basically just syntactic sugar for:

    Foo::bar("Foo", $baz)

    with one key exception: in the first example, if the "Foo" package doesn't have a sub called "bar", then the object-oriented concept of inheritance is used to search for it in some other packages.

    So in your example, the list of keys accepted by StatView::Script->new can probably be seen by examining the new sub in the StatView::Script package.

      Foo::bar("Foo", $baz) what is Foo here. Is it a name of a routine or just a typical scalar value?

        The first Foo is the name of a package, as in...

        package Foo; sub bar { print "Inside the package, I am called 'bar'\n"; print "But outside the package, I am called Foo::bar\n"; }

        The second "Foo" is just a string scalar.

        I really think you need to read perlboot from start to finish.

Re: Perl "new" command
by nemesdani (Friar) on Mar 01, 2012 at 07:27 UTC
    Since the title of the node is "Perl "new" command", we should also add that in Perl the constructor doesn't have to be named "new". It's just a common thing to do, which helps people understand each other's code better.

      n Perl the constructor doesn't have to be named "new"

      In Perl no, but OO frameworks like Moose and its derivatives have adopted this convention and use "new" for object instantiation.

      Principle of Least Astonishment: Any language that doesn’t occasionally surprise the novice will pay for it by continually surprising the expert

      Actually, constructors cannot be named new. The name of the constructor in Perl is called bless.

      Unless you make an XS function called new which reimplements bless.

        I thought a constructor is any routine that calls the bless operator.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://957109]
Approved by davies
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2014-07-29 22:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (229 votes), past polls