Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

my

by malaga (Pilgrim)
on Feb 03, 2001 at 04:35 UTC ( [id://56159]=perlquestion: print w/replies, xml ) Need Help??

malaga has asked for the wisdom of the Perl Monks concerning the following question:

i'm not sure if i'm asking this right, but if i'm using strict, so i have to use my, then what do i do to pass references between sub scripts?

Replies are listed 'Best First'.
Re: my
by doran (Deacon) on Feb 03, 2001 at 06:30 UTC
    I'm probably just dumb (okay, probably not probably), but it took me way too long to understand the difference between package variable and lexical variables.

    An article that helped me greatly is one by Dominus called Coping with Scoping. Like I said, I found it very useful (as is often the case with Dominus' articles 'n stuff). Check it out if the above answers still leave you wondering, or even if they don't.

    good luck
      how come all dominus' stuff is dead links? i found the information on his site, but the his links from perlmonk aren't working. i got downvoted for asking, i guess. i have to be more careful.
        Dunno. You'll have to ask Dominus. It was working yesterday. Maybe he's upgrading. ;)

Re: my
by autark (Friar) on Feb 03, 2001 at 04:44 UTC
    You don't have to use my when you use strict. If you predeclare your variables using use vars (or our() with newer perls), or if you fully qualify your variables ($main::foo), use strict will not complain.

    Since I'm not sure what you mean by sub scripts, I can't fully answer your question, could you please specify more clearly what you mean by sub scripts ?

    Autark.

Re: my
by malaga (Pilgrim) on Feb 03, 2001 at 05:06 UTC
    oh, and thanks, i'll look up "use" and the rest.

    update: why would i get downvoted for saying thank you?
      You may also want to check out this node that I wrote up a while back on passing variables as arguments to a subroutine.

      -marius
Re: my
by malaga (Pilgrim) on Feb 06, 2001 at 04:10 UTC
    now it's working. i don't know what that was about, but all of those hyperlinks were dead when i tried, except the one to his site.

      The general hint is if a webpage (website) is down, you can look on google or something for a cached version. In this case, it's (google) Coping with Scoping.

      The syntax to use for google's cache is:

      http://www.google.com/search?q=cache:[hostname/page]
Re: my
by malaga (Pilgrim) on Feb 03, 2001 at 04:47 UTC
    sorry, i meant subroutine.
      There is no difference in using dynamic vs. lexical variables as parameters to a function. Assuming perl5.6 (or greater):
      use strict; sub foo { my $foo = shift; print $foo; } my $my_var = "lexical bar"; our $our_var = "dynamic bar"; foo($my_var); foo($our_var);
      However, using dynamic variables may cause strange results if not used with care. So using lexical variables is not a bad idea at all.

      Using references to lexical and dynamic variables is just the same, except that you have to reference and dereference the variables.

      But please note that if you're not careful enough using dynamic variables (those not declared with my), strange results can appear, so if you can, use lexical variables - that's probabely what you want anyway.

      Autark.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-03-29 13:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found