Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Package problems

by kennethk (Abbot)
on Apr 14, 2017 at 22:15 UTC ( #1187979=note: print w/replies, xml ) Need Help??


in reply to Package problems

Anonymous Monk 2 above is right in the general sense that you probably want to familiarize yourself with a Perl object framework like Moose, but that is not the answer to your question. Likewise, strict would help but does not answer your question (see Use strict warnings and diagnostics or die).

When you call Dog::play ($name); you are not passing the idea of a variable called $name to Dog::play, but the contents the the variable in current scope. You call Dog::play ($name); in the Cat package, and it has variable called $name. So Perl inserts Garfield into the argument list.

Of course, this is further confused by your use of our and lack of scope declaration for $name = "Garfield";. If instead you write our $name = "Garfield";, you can now get the two different results by calling

Dog::play ($Dog::name); # Prints Odie catches a tennis ball.
or
Dog::play ($name); # Prints Garfield catches a tennis ball.

The comments about better methodology would have helped catch and clarify these issues a number of different ways.


#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2020-11-25 03:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?