Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > how to: create/use/modify shared variable in Perl 5.6.1

Thread Tools

how to: create/use/modify shared variable in Perl 5.6.1

Posts: n/a
Hello group,

Following is the workarround to implement multithread in Perl 5.6.1

1. i loop through a hash(for eg.) to check existence of the file
2. if the file does not exists
2.1 make an entry in SHARED variable
mentioning that a process has been spawned to create this perticular
file(eg. bangalore.xml)

2.2 spawn a process to create a file(for eg.
2.2.1 in the child process
create a file
2.2.2 after creating file,
make a note in SHARED variable that child is exiting after creating a

if file exists
close file-handle

NOTE: Parent should not wait for child to finish its job, because we
have more one file(location) to look up. And we cannot wait for the
one child creating bangalore.xml to finish, to spawn a child to create
sofia.xml file

Where im stuck?
I don't know to use create and use shared variable in Perl :

Can any one help me here pls.

here is the code.
#---- -------
package SBREnviron;

'bangalore' => {
'xml_file' => q(bangalore.xml)
'sofia' => {
'xml_file' => q(sofia.xml)


# -------- -------------
require '';

use FileHandle;

%process_track= ();
while(my ($k, $v) = each %SBREnviron::LOCATIONS){
my $fh = new FileHandle;
my $dbh;

print "\nPARENT($foo): open_file: trying to open
$SBREnviron::LOCATIONS{$k}->{'xml_file'} ";
open ($fh, $SBREnviron::LOCATIONS{$k}->{'xml_file'}) or die"ERROR:
#if ($@ =~ /ERROR: file does not exists/){
if ( ($@ =~ /ERROR:/)){
print "\nPARENT($foo): file_exists: NO";
print "\n\tPARENT($foo): checking for spawned process";

if (not exists $process_track{$k}){
print "\n\tPARENT: isSpawned: NO";
#print "\n\tPARENT($foo): no process is spawned for $k, so im
going to spawn a child to create $SBREnviron::LOCATIONS{$k}-
>{'xml_file'} file.";

# make a note that a process has spawn for this already
$process_track{$k} = 1;
print "\nPARENT: shared_var(so called): $process_track{$k}";
my $pid;
$pid = fork();
if ($pid == 0){
# child process
print "\n\t---\n\tCHILD:im the child, and will create
$SBREnviron::LOCATIONS{$k}->{'xml_file'} file in a short while\t\t---
sleep 15;
# just a create a file
my $fh = new FileHandle;
open ($fh, ">$SBREnviron::LOCATIONS{$k}->{'xml_file'}");

# delete the entry from hash, representing the child process has
print "\n\t---\n\tCHILD: im deleting an entry $k\t\t---";
delete $process_track{$k};
print "\nPARENT: shared_var(so called): $process_track{$k}" if
(exists $proces_track{$k});
#process will exit once it is finished.
} # end of child process
print "\n\tPARENT: isSpawned: YES";#print "\nPARENT($foo): a
process is already running for this $k";
print "\nPARENT: shared_var(so called): $process_track{$k}";
} # end of if-file-doesnot-exists
print "\n PARENT($foo): $SBREnviron::LOCATIONS{$k}->{'xml_file'}
exists, so closing the filehandle"};
} # end of loop through locations
print "\n\n";
}# end of infinite loop

Thanks and regards,

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
global variable is shared in shared library C Programming 1 07-15-2008 04:26 PM
Shared Public Variables and Shared Methods Joe Fallon ASP .Net 3 07-16-2004 07:11 AM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM
Shared Variable Vs. Session Variable John Kraft ASP .Net 2 10-21-2003 07:56 PM