Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

$class name not pass in the new()

by perldood (Initiate)
on Mar 12, 2012 at 03:19 UTC ( #959058=perlquestion: print w/ replies, xml ) Need Help??
perldood has asked for the wisdom of the Perl Monks concerning the following question:

In file Animals.pm, I have package Cow; sub Cow::speak { my $class = shift; print "$class Cow goes moooo!\n"; } package Horse; sub Horse::speak { my $class = shift; print "$class Horse goes neigh!\n"; } package Sheep; sub Sheep::speak { my $class = shift; print "$class Sheep goes baaaah!\n"; } 1;

And calling script works like this. first code can print value of $class and second code doesnt, why ??

Working code: #!/usr/bin/perl use lib '/home/perldood/Custom/perl/lib/perl5/Animals'; use Animals; Cow->speak; Horse->speak; Sheep->speak; Non working code: #!/usr/bin/perl use lib '/home/perldood/Custom/perl/lib/perl5/Animals'; use Animals; Cow::speak; Horse::speak; Sheep::speak; Can you please explain, Why ??
Thanks

Comment on $class name not pass in the new()
Select or Download Code
Re: $class name not pass in the new()
by Khen1950fx (Canon) on Mar 12, 2012 at 03:48 UTC
Re: $class name not pass in the new()
by NetWallah (Abbot) on Mar 12, 2012 at 04:03 UTC
    If you "use warnings;" in the package, you get these helpful messages:
    Use of uninitialized value $class in concatenation (.) or string at An +imal.pl line 7. Cow goes moooo! Use of uninitialized value $class in concatenation (.) or string at An +imal.pl line 13. Horse goes neigh! Use of uninitialized value $class in concatenation (.) or string at An +imal.pl line 19. Sheep goes baaaah!
    for the stuff you say is "Non working code".

    This gives a clear hint on what $class is set to when invoked that way, and why it "does not work".

                 All great truths begin as blasphemies.
                       ― George Bernard Shaw, writer, Nobel laureate (1856-1950)

Re: $class name not pass in the new()
by chromatic (Archbishop) on Mar 12, 2012 at 07:01 UTC

    These are function calls. The only arguments the functions receive are those you pass manually:

    Cow::speak; Horse::speak; Sheep::speak;

    These are method calls. They receive their invocants as arguments, along with any arguments you pass manually:

    Cow->speak; Horse->speak; Sheep->speak;

    You can call methods as functions, but you will break things if you do.


    Improve your skills with Modern Perl: the free book.

      Thanks guys !! Awesome explanation...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (9)
As of 2014-12-26 06:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (167 votes), past polls