Home > Archive > PERL Beginners > December 2007 > Use of `my'
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| reader@newsguy.com 2007-12-10, 7:01 pm |
| I'm trying to convert over from yrs of not bothering to use `my var;' for
variables in my scripts (All are less than 300 lines)
It seems to be a big pita for the most part with no real gain. Given
the brief and simple nature of my scripting needs. But that is
probably due to developing a sloppy style.
I'm not wanting to start a discussion about the value of using `my'
here, just looking for some advice on its use.
With a script that searches out specific regex from files that may be
buried in a complex tree, I want to print the hits of course but also
want to keep up with the filenames and directories searched. (only the
unique directories).
I'm not finding a simple and reliable way to use `my' in keeping with
the general reasons to do so.
My usage below is not in line with what is advocated here about using
`my' and I'd like for it to get closer to that goal.
There is a lot of declaring in main that should be more localized. So
I'm looking for pointers about how to correct it.
I've declared `my %data;' in main because the uniqifyer code inside
`wanted()' fails if declared inside `wanted()' and a directory path
for each file found gets collected so repeated collection of same path
occurs. How do I get around that?
I've declared `@SearchedDir' in main because I want to use it in
`report()'.
So trying to keep this as simple as I can, the code runs something
along this line:
#!/usr/bin/perl -w
use strict;
use warnings;
use File::Find;
[...] too many lines of code, including a getopts section, setting up
search parameters, tests etc. go here
my $SrchTree = shift;
find(\&wanted, $SrchTree);
my (%data, @SearchedDir);
sub wanted {
## Collect only uniq paths
if ($data{$File::Find::dir}++ == 0) {
## Make an array of all (Unique) directories searched
push @SearchedDir, $File::Find::dir . "\n";
}
[...] Too many more lines of processing that gather the lines
matching regex and the matching filename (printed as we go)
go here.
}
# Now print a summary
print report();
(Note: All vars but `ShowStr' below, were declared in main so
they could be used here)
sub report {
## Return how many hits.. how many files searched, where
## and the regex used.
my $ShowStr = " ========================================
=====
<$HitCnt> hits found in <$GlobalCnt> files
Using directorie[s]:
@SearchedDir
Regex used: <$ShowInFileRe>
Filename filter: <$FnameFilter>
========================================
=====\n";
return $ShowStr;
}
| |
|
|
| Paul Johnson 2007-12-10, 7:01 pm |
| On Mon, Dec 10, 2007 at 11:12:22AM -0600, reader@newsguy.com wrote:
> I'm trying to convert over from yrs of not bothering to use `my var;' for
> variables in my scripts (All are less than 300 lines)
>
> It seems to be a big pita for the most part with no real gain. Given
> the brief and simple nature of my scripting needs. But that is
> probably due to developing a sloppy style.
>
> I'm not wanting to start a discussion about the value of using `my'
> here, just looking for some advice on its use.
I suspect I'll get lynched, but if you are happy with your style and it
works for you then feel free to stick with it. If it's more pain than
it's worth, then don't do it. However, giving it a good chance, as you
seem to be doing, is a sensible decision.
Also, remember that "vars" is only one third of "use strict". Even if
you don't want that part, you might find value from the other parts.
> My usage below is not in line with what is advocated here about using
> `my' and I'd like for it to get closer to that goal.
>
> There is a lot of declaring in main that should be more localized. So
> I'm looking for pointers about how to correct it.
>
> I've declared `my %data;' in main because the uniqifyer code inside
> `wanted()' fails if declared inside `wanted()' and a directory path
> for each file found gets collected so repeated collection of same path
> occurs. How do I get around that?
I don't think you do. Sometimes a module's interface is not everything
it might be, and you just have to live with that.
> I've declared `@SearchedDir' in main because I want to use it in
> `report()'.
But there is more to scoping than subroutine and global scope. Perhaps
something like the following might help, by keeping variable to the
smallest scope necessary:
#!/usr/bin/perl
....
{
my (%data, @SearchedDir);
sub wanted {
...
}
sub report {
...
}
}
....
--
Paul Johnson - paul@pjcj.net
http://www.pjcj.net
| |
| reader@newsguy.com 2007-12-10, 7:01 pm |
| Gunnar Hjalmarsson <noreply@gunnar.cc> writes:
> reader@newsguy.com wrote:
>
> http://perl.plover.com/FAQs/Namespaces.html
>
Having been pointed to that url and another I've lost now is what
prompted this most recent post about it. I didn't find answeres
there.
Descibing why you should and what it does isn't the same as telling
how to make it happen with specific code.
|
|
|
|
|