Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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.
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?

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (9)
As of 2020-08-12 16:22 GMT
Find Nodes?
    Voting Booth?
    Which rocket would you take to Mars?

    Results (66 votes). Check out past polls.