Passing variables into another script

Discussion in 'Perl Misc' started by Ken Soon, May 21, 2007.

  1. Ken Soon

    Ken Soon Guest

    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?
    Ken Soon, May 21, 2007
    #1
    1. Advertising

  2. Ken Soon

    Ian Wilson Guest

    Ken Soon wrote:
    > 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";

    >./t1.pl

    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.
    Ian Wilson, May 21, 2007
    #2
    1. Advertising

  3. Ken Soon

    Sisyphus Guest

    "Ken Soon" <> wrote in message
    news:f2roa4$...
    ..
    ..
    > 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
    Sisyphus, May 21, 2007
    #3
  4. Ken Soon

    Dr.Ruud Guest

    Ken Soon schreef:

    > Is there anyway to circumvent this problem?


    Use %ENV.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, May 21, 2007
    #4
  5. On Mon, 21 May 2007 17:17:39 +0800, Ken Soon wrote:

    > 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
    Martijn Lievaart, May 21, 2007
    #5
  6. Martijn Lievaart <> wrote:

    > Try:
    >
    > system "side1.pl", "$directory", "$dump", "$ftc";



    Try,

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


    > and read perldoc -f system.



    and read perldoc -q vars.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, May 21, 2007
    #6
  7. Ken Soon

    Ken Soon Guest

    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
    Ken Soon, May 22, 2007
    #7
  8. Ken Soon <> wrote:


    > Oh also sorry I didn't mentioned I am using Perl in windows so not sure
    > about %ENV and other Unix-related stuffs.



    %ENV is Perl related, not Unix-related.


    > but I ran into problems of dynamic hashes



    What is a static hash?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, May 22, 2007
    #8
  9. Ken Soon

    Ken Soon Guest

    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!

    "Tad McClellan" <> wrote in message
    news:...
    > Ken Soon <> wrote:
    >
    >
    >> Oh also sorry I didn't mentioned I am using Perl in windows so not sure
    >> about %ENV and other Unix-related stuffs.

    >
    >
    > %ENV is Perl related, not Unix-related.
    >
    >
    >> but I ran into problems of dynamic hashes

    >
    >
    > What is a static hash?
    >
    >
    > --
    > Tad McClellan SGML consulting
    > Perl programming
    > Fort Worth, Texas
    Ken Soon, May 22, 2007
    #9
  10. [ 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 <> wrote:
    > "Tad McClellan" <> wrote in message
    > news:...
    >> Ken Soon <> wrote:


    >>> but I ran into problems of dynamic hashes

    >>
    >>
    >> What is a static hash?



    > Because I used hashes whose name changes to hold different keys and values.



    You can use a hash reference instead then.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, May 22, 2007
    #10
  11. On Mon, 21 May 2007 17:50:18 -0500, Tad McClellan wrote:

    > Martijn Lievaart <> wrote:
    >
    >> Try:
    >>
    >> system "side1.pl", "$directory", "$dump", "$ftc";

    >
    >
    > 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
    Martijn Lievaart, May 22, 2007
    #11
  12. Ken Soon

    Joe Smith Guest

    Ken Soon wrote:

    > 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";
    Joe Smith, May 22, 2007
    #12
  13. Ken Soon

    Ken Soon Guest

    > 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?
    Ken Soon, May 23, 2007
    #13
  14. Ken Soon

    Joe Smith Guest

    Ken Soon wrote:
    >> Don't use hash variables whose names are variable. Use a hash of hashes ...
    >> $data{'user'}{'joe'} = 'System admin';
    >> $data{'group'}{'power'} = 'Power users';

    >
    > 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}};
    Joe Smith, May 23, 2007
    #14
  15. Ken Soon

    Ken Soon Guest

    > 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 :)
    Ken Soon, May 23, 2007
    #15
  16. Ken Soon <> wrote:

    > 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


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, May 23, 2007
    #16
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    9
    Views:
    935
  2. Rajat
    Replies:
    3
    Views:
    689
    Jorgen Grahn
    Jan 8, 2010
  3. VYAS ASHISH M-NTB837
    Replies:
    2
    Views:
    551
    Jan Kaliszewski
    Jan 7, 2010
  4. Robert Cohen
    Replies:
    3
    Views:
    265
    Andrew Durstewitz
    Jul 15, 2003
  5. vikrant
    Replies:
    4
    Views:
    273
Loading...

Share This Page