Passing variables into another script

K

Ken Soon

I have got a main script to run 2 scripts inside. I will be passing the same
directories into both of the script

Main.pl
print "Please enter the directory path: ";
$directory =<>;
chomp($directory);

print "Please enter the directory for the summary file to be stored : ";
$dump =<>;
chomp($dump);

print "Please enter the directory of the files: ";
$ftc = "C:\\PERL";
chomp($ftc);

system "side1.pl '$directory' $dump $ftc";
system "side2.pl $directory $dump $ftc";

Side1.pl
($directory, $dump, $ftc) = @ARGV;

print "$directory\n";
print "$dump\n";
print "$ftc\n";

Let's say $directory is C:\documents and settings\csoon\file
However, what was printed is
C:\documents
and
settings\csoon\file
and the $dump and $ftc were not printed.
So yah understand that @ARGV separate by the space.
Is there anyway to circumvent this problem?
Also is there any other related kind of problem that I should be aware of
when dealing passing variables into another script?
 
I

Ian Wilson

Ken said:
I have got a main script to run 2 scripts inside. I will be passing the same
directories into both of the script

system "side1.pl '$directory' $dump $ftc";
system "side2.pl $directory $dump $ftc";

Side1.pl
($directory, $dump, $ftc) = @ARGV;

print "$directory\n";
print "$dump\n";
print "$ftc\n";

Try
print "directory = '$directory'\n";
print "dump = '$dump'\n";
print "ftc = '$ftc'\n";

Let's say $directory is C:\documents and settings\csoon\file
However, what was printed is
C:\documents
and
settings\csoon\file
and the $dump and $ftc were not printed.

Yes they were printed, they just didn't contain what you wanted them to.
So yah understand that @ARGV separate by the space.
Is there anyway to circumvent this problem?

Quoting the variables. Depends on your shell I expect.
cat t1.pl
#!/usr/bin/perl
use strict; use warnings;
system "./t2.pl aaa 'bbb ccc' ddd";
cat t2.pl
#!/usr/bin/perl
use strict; use warnings;
my($x, $y, $z) = @ARGV;
print "x='$x'\ny='$y'\nz='$z'\n";
x='aaa'
y='bbb ccc'
z='ddd'
Also is there any other related kind of problem that I should be aware of
when dealing passing variables into another script?

I wouldn't pass user-supplied data to system() without first sanitising
it. Suppose the user entered a $ftc value of '/foo; rm /*'.

I'd check the result of the system call to see if it succeeded or failed.

I'd make side1.pl and side2.pl into modules and avoid the whole messy
business of using system to invoke a shell to invoke a perl interpreter
to run another perl program.
 
S

Sisyphus

..
..
Let's say $directory is C:\documents and settings\csoon\file
However, what was printed is
C:\documents
and
settings\csoon\file
and the $dump and $ftc were not printed.

Strictly speaking, $dump and $ftc *were* printed.
$directory is 'C:\documents'.
$dump is 'and'.
$ftc is 'settings\csoon\file'.
So yah understand that @ARGV separate by the space.
Is there anyway to circumvent this problem?
Also is there any other related kind of problem that I should be aware of
when dealing passing variables into another script?

C:\_32\pscrpt>type try.pl
print "\$ARGV[0]: $ARGV[0]\n";
print "\$ARGV[1]: $ARGV[1]\n";
print "\$ARGV[2]: $ARGV[2]\n";

C:\_32\pscrpt>perl try.pl this and that
$ARGV[0]: this
$ARGV[1]: and
$ARGV[2]: that

C:\_32\pscrpt>perl try.pl 'this and that'
$ARGV[0]: 'this
$ARGV[1]: and
$ARGV[2]: that'

C:\_32\pscrpt>perl try.pl "this and that"
$ARGV[0]: this and that
$ARGV[1]:
$ARGV[2]:

So you need to get "Documents and Settings" wrapped in *double* quotes.
Perhaps that can be achieved with (untested):

system "side1.pl \"$directory\" $dump $ftc";

Cheers,
Rob
 
M

Martijn Lievaart

Let's say $directory is C:\documents and settings\csoon\file However,
what was printed is
C:\documents
and
settings\csoon\file
and the $dump and $ftc were not printed. So yah understand that @ARGV
separate by the space. Is there anyway to circumvent this problem? Also
is there any other related kind of problem that I should be aware of
when dealing passing variables into another script?

Try:

system "side1.pl", "$directory", "$dump", "$ftc";
system "side2.pl", "$directory", "$dump", "$ftc";

and read perldoc -f system.

HTH,
M4
 
K

Ken Soon

Ah thanks alot people.
Think Martin's is short and sweet. :)
Oh also sorry I didn't mentioned I am using Perl in windows so not sure
about %ENV and other Unix-related stuffs.

This didn't work though cause i guess $dump and $ftc were already assigned
"and" & "settings\csoon\file".
Try
print "directory = '$directory'\n";
print "dump = '$dump'\n";
print "ftc = '$ftc'\n";

Oh yah I wanted to sub modules both the side1.pl and side2.pl into main.pl
but I ran into problems of dynamic hashes not being to hold any data so I
have to restart the operations for both side scripts.\

Ken
 
K

Ken Soon

Ah Sorry, made a mistake here about %ENV.

Because I used hashes whose name changes to hold different keys and values.
Well that's the name I have been told by my friend though. Not sure if it is
the right way to call it.

ah thanks Tad too.
system "side1.pl", $directory, $dump, $ftc;
works well too!
 
T

Tad McClellan

[ Please do not post upside down. Put your comments following the
text that you are commenting on, like everybody else here does.
Please do not quote .sigs.
]


Ken Soon said:
Because I used hashes whose name changes to hold different keys and values.


You can use a hash reference instead then.
 
M

Martijn Lievaart

Try,

system "side1.pl", $directory, $dump, $ftc;

Raaaaaaghhhhhh, I do it again. And only in this group I swear, never in
my own programs..... :-(

Thanks for the correction.

M4
 
J

Joe Smith

Ken said:
Oh yah I wanted to sub modules both the side1.pl and side2.pl into main.pl
but I ran into problems of dynamic hashes not being to hold any data so I
have to restart the operations for both side scripts.\

Don't use hash variables whose names are variable. Use a hash of hashes instead.
Use a global (static) variable, not a lexical (my) variable.

Instead of creating %user and %group on the fly:

package main;
our %data;
$data{'user'}{'joe'} = 'System admin';
$data{'group'}{'power'} = 'Power users';

package side1;
print "User $user is $main::data{'user'}{$user}\n";
print "Group $group is $main::data{'group'}{$group}\n";
 
K

Ken Soon

Don't use hash variables whose names are variable. Use a hash of hashes
instead.
Use a global (static) variable, not a lexical (my) variable.

Instead of creating %user and %group on the fly:

package main;
our %data;
$data{'user'}{'joe'} = 'System admin';
$data{'group'}{'power'} = 'Power users';

package side1;
print "User $user is $main::data{'user'}{$user}\n";
print "Group $group is $main::data{'group'}{$group}\n";

Yup, have used global variable and not (my) variable for my script

Hmm, $data{'user'}{'joe'} = "System admin' is something like a 2D hash or
something?
 
J

Joe Smith

Ken said:
Hmm, $data{'user'}{'joe'} = "System admin' is something like a 2D hash or
something?

Yes, that is what I meant by a "hash of hashes". See "perldoc perlref".
-Joe

$data{user}{'joe'} = 'System Admin';
$data{user}{'guest'} = 'Guest User';
$data{group}{'Administrators'} = 544;
$data{group}{'Power Users'} = 547;

sub print_sorted {
my %hash = @_;
print " $_ = $hash{$_}\n" for sort keys %hash;
print " $hash{$_} = $_\n" for sort {$hash{$a} cmp $hash{$b}} keys %hash;
}

print "Users:\n"; print_sorted %{$data{user}};
print "Groups:\n"; print_sorted %{$data{group}};
 
K

Ken Soon

Yes, that is what I meant by a "hash of hashes". See "perldoc perlref".
-Joe

$data{user}{'joe'} = 'System Admin';
$data{user}{'guest'} = 'Guest User';
$data{group}{'Administrators'} = 544;
$data{group}{'Power Users'} = 547;

sub print_sorted {
my %hash = @_;
print " $_ = $hash{$_}\n" for sort keys %hash;
print " $hash{$_} = $_\n" for sort {$hash{$a} cmp $hash{$b}} keys
%hash;
}

print "Users:\n"; print_sorted %{$data{user}};
print "Groups:\n"; print_sorted %{$data{group}};

ahhh.. Thanks alot Joe, will try it out in future scripts :)
 
T

Tad McClellan

Ken Soon said:
Hmm, $data{'user'}{'joe'} = "System admin' is something like a 2D hash or
something?


perldoc -q structure
perldoc perlreftut
perldoc perlref
perldoc perllol
perldoc perldsc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top