CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Trying to understand our versus my



Sep 29, 2015, 11:22 AM

Post #1 of 5 (1392 views)
Trying to understand our versus my Can't Post

use strict;
use FindBin;
use lib '/packg/perl/lib/5.8.0';
use lib '/packg/lib';
our $strDate=qx{date '+%m-%d-%y'};
my @servers=qw(
foreach my $box (@servers) {
print "Connecting to $box...\n";
my $out = `"ssh $box /usr/sbin/filequery -a"`;
print $out;

This works fine except that I have to change the our next to $StrDate to my for it to run. Why would I use our?
I keep seeing information on the perl tutorial sites that indicates that our is for packages. But I am not understanding what that means though I have tried to run test scripts.
when would our be used, for example in the script above? or is it needed only for certain conditions? I've checked out perlmonks but I am finding it really hard to understand.

Anyone can help with simple examples?


Sep 29, 2015, 12:30 PM

Post #2 of 5 (1387 views)
Re: [regex2012] Trying to understand our versus my [In reply to] Can't Post

Both my and our declare variables with exactly the same scope. The my variable is a lexical variable. The our variable is a global variable. If the variable name is only declared once, it should not make any difference which you use. The difference occurs when the variable is declared more than once (in different scopes). Every instance of the our variable refers to same variable. Every instance of the my variable is a different variable. They have nothing to do with each other except that they share the same name.

I would not expect it to make a difference in you example unless the variable name is used in the module. In that case, the our variable could conflict with it, but the my variable could not.
Good Luck,

Veteran / Moderator

Sep 29, 2015, 1:15 PM

Post #3 of 5 (1385 views)
Re: [regex2012] Trying to understand our versus my [In reply to] Can't Post


there is no reason at all to use our in the script above. And, in general, there is usually no reason to use our in a standalone script. Even if you want (and really need) your variable to be "global" within your standalone script, you would still be advised in most cases to declare it with my near the top of your script and outside any block.

In theory, there are some very rare exceptions to this (for example if you want to localize a variable, it has to be a package global, but I have yet to see a practical case where this is really useful, or in the event you wanted to use symbolic references, but they are deprecated and you should most probably not do it), and, since you ask the question, your level of comprehension of these matters is such that you can simply avoid our variables altogether in stand-alone scripts.

You would possibly need our when you start to write modules, especially if you want some of the module variables to be accessible from the calling program or possibly from some other module, since lexical variables (declared with my) in a module cannot be accessed from a program in another file (the scope of lexicals is limited at most to the file in which they are declared). Even then, it is IMHO often better to provide access through a subroutine or a method, but it is sometimes easier to use a few package globals. Also, there are some magical constructs requiring the use of package globals.

In brief, our variables are relatively seldom used (except for some common boiler plate code at the top of modules), and the case outlined above of accessing to variables from another compilation unit (e.g. accessing a module variable from the calling program) is essentially the only one where you would really need our variables (unless you start to write highly magical stuff, but then you most probably don't need my advice).


Sep 29, 2015, 7:02 PM

Post #4 of 5 (1373 views)
Re: [Laurent_R] Trying to understand our versus my [In reply to] Can't Post

One fairly common exception to Laurent's advice is the use of our to initialize a variable in a BEGIN block. A lexical variable would not be available outside that block. I often need this capability when using the -p or -n runtime options.
Good Luck,


Sep 30, 2015, 7:07 AM

Post #5 of 5 (1352 views)
Re: [BillKSmith] Trying to understand our versus my [In reply to] Can't Post

Thanks for everyone's very detailed replies!! I really appreciate it.


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives