Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Variables and Scope: The battle begins

by GrandFather (Saint)
on Dec 12, 2013 at 02:56 UTC ( [id://1066759]=note: print w/replies, xml ) Need Help??


in reply to Variables and Scope: The battle begins

Great story if we had all night around the camp fire and something to roast on sticks while we swapped tales, but there's no sample code that I can run to reproduce your result and too much required context for me to be able to tell where things are going pear shaped for you.

I can make a suggestion or two though, but if they don't help you'll have to reduce your code to a small stand alone sample script the reproduces the bug.

  1. I suspect you are doing this anyway, but just in case: always use strictures (use strict; use warnings;).
  2. Don't use the &subname calling style. Use subname() instead.
  3. If you are using subroutine prototypes, don't!
True laziness is hard work
  • Comment on Re: Variables and Scope: The battle begins

Replies are listed 'Best First'.
Re^2: Variables and Scope: The battle begins
by Shadow-Master (Initiate) on Dec 12, 2013 at 15:56 UTC
    Thank you for the fast reply.
    1) I always use strict and warnings. :)
    2) All my subs are called with arguments, so the ()'s are always there, but given that I just saw the difference clearly delineated for me, I will remove the &'s in my code and see if that changes. THank you.
    3) I had to google what a subroutine prototype was. I can tell you with certainty that I have none of those. :)
      All my subs are called with arguments, so the ()'s are always there

      Actually, no they aren't ;). Your code includes &getinput; and &status calls without arguments at least. Just goes to show how easy it is for code not to be the way you think it is!

      If you haven't resolved the issue yet you should try to strip out code until you find the problem or you have a sample script with a minimum of irrelevant code and still demonstrating the issue. You may find I know what I mean. Why don't you? helps focus your effort.

      True laziness is hard work
        You are right, neither of those have arguments. status neither takes any nor returns any, it just prints the status of several variables. getinput returns an argument (the input). Neither affect global variables.
        However, you are right in that code can often be misleading. I have stripped all the &'s from my code and confirmed that it still functions like I expected it to. I have yet to try again with global vars, so we will see how that goes.
Re^2: Variables and Scope: The battle begins
by educated_foo (Vicar) on Dec 12, 2013 at 06:21 UTC
    How do any of those things you mention help the OP?

      For the OP's immediate problem it's likely they don't. However the type of issue the OP is having leads me to think that the OP hasn't wide programming experience so pointing out areas that are a frequent cause of subtle problems may help. For example the & calling convention leads to

      doit(1, 2, 3); againSam(1, 2, 3); sub againSam { andAgain(); andAgain(); } sub doit { &andAgain; &andAgain; } sub andAgain { my (@values) = @_; print "<@values>\n"; }

      printing:

      <1 2 3> <1 2 3> <> <>

      which may be surprising if you don't appreciate what &andAgain is doing. Mixing prototypes in may also be surprising:

      sub andAgain ($); my @nums = (1, 2, 3); andAgain(@nums); &andAgain(@nums); sub andAgain ($) { my (@values) = @_; print "<@values>\n"; }

      prints:

      <3> <1 2 3>

      I doubt either "issue" is what is troubling the OP currently. However & call usage at least is present in the OP's code and where there is & calling there are likely prototypes. Pointing out potential problem areas in the OP's code, even if it's not what the OP is asking about, helps the OP. Until the OP shows us some code that reproduces the actual problem the best we can do is offer help with a few style issues.

      Note, the example code really isn't for educated_foo whom I'm sure knows all about this stuff. The examples are for those who are wondering what the fuss is about. Oh, and strictures are always useful, at the very least to pick up typos and brain farts.

      True laziness is hard work

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-26 05:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found