gctaylor1 has asked for the wisdom of the Perl Monks concerning the following question:
Last week I received great advice here about using dispatch tables so I could add more command line initiated actions to my script.
Currently my script operates in
a linearly fashion from top to bottom and has one main thing it does. I have
all of my subroutines at the bottom and to put together the script I just call
subroutines and walk through the individual tasks I want to complete until done.
I was
thinking that in order to use dispatch tables all I should do is put my
current script inside a new major subroutine which would continue to call other
existing minor subroutines as it does now. Then I would eventually add additional major
subroutines and pick and choose from the minor subroutines to complete that
action until done.
The problem is that when I put my main script in a subroutine
I get all kinds of "Variable $x will not stay shared at script.pl line nnn"
errors.
- Is there a rookie friendly way to wrap my current script in a subroutine and not get the "Variable will not be shared" error?
The research I've done leads me to anonymous subroutines, closures, and much confusion. This example below isn't really the best example without including my whole script but it might be a good enough example because of the subroutine reference of File::Find \&wanted and it does give me the same error. There are several issues I'm dealing with in this example.
#!/usr/bin/perl use strict; use warnings; use File::Find; my %actions_hash = define_actions(); if (!defined @ARGV ) { @ARGV = "-h"; } my $action = shift; # First argument my @args = @ARGV; # Remaining arguments if (exists $actions_hash{$action}) { $actions_hash{$action}->(@args); } else { $actions_hash{_default_}->(@args); } sub define_actions { return ( '-f' => \&findfile, ); } sub findfile { my $target = $args[0]; my $location = "/tmp/dir1"; my $filename = find \&wanted, "$location"; sub wanted { (my $foundfile = $File::Find::name) if $File::Find::name =~ $t +arget; return $foundfile; } print $filename; }
For the record I've looked at the tutorials here but can't seem to apply the knowledge to my script.
Thank-you for your consideration.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Adding a dispatch table and getting "Variable $x will not stay shared" errors.
by GrandFather (Saint) on Mar 03, 2009 at 01:15 UTC | |
|
Re: Adding a dispatch table and getting "Variable $x will not stay shared" errors.
by tilly (Archbishop) on Mar 03, 2009 at 06:13 UTC | |
|
Re: Adding a dispatch table and getting "Variable $x will not stay shared" errors.
by runrig (Abbot) on Mar 03, 2009 at 01:12 UTC | |
by gctaylor1 (Hermit) on Mar 03, 2009 at 02:02 UTC | |
by runrig (Abbot) on Mar 03, 2009 at 16:30 UTC | |
|
Re: Adding a dispatch table and getting "Variable $x will not stay shared" errors.
by Lawliet (Curate) on Mar 03, 2009 at 01:02 UTC |