Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Problem passing a string in system function

by Anonymous Monk
on May 19, 2006 at 10:28 UTC ( [id://550460]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, How do you pass a string inside an under system call. I tried to pass a string like this under EMAIL option:
system("perl mycode.pl -email 'test_string' -type $sometype -unit $so +me_unit");
or
my $receipt_email = 'myemail@gmail.com" system("perl mycode.pl -email $receipt_email -type $sometype -unit $s +ome_unit");
But while debugging for mycode.pl I can't see the value for under EMAIL option, while I can see the value for TYPE and UNIT option.

Replies are listed 'Best First'.
Re: Problem passing a string in system function
by blazar (Canon) on May 19, 2006 at 10:36 UTC

    I see no obvious reason why it shouldn't work. You may try using the multiple arguments version of system, though. From perldoc -f system:

    If there is only one scalar argument, the argument is checked for shell metacharacters, and if there are any, the entire argument is passed to the system's command shell for parsing

    But while we're here I feel like repeating the (now) daily question: why don't you make mycode.pl into a real module?

      Ok, actually the system call is run under Acme::Spork.
      Could it be the cause?
      use Acme::Spork; my $req_id = spork( sub { system("perl mycode.pl -email \"$uemail\" +-type $sometype -unit $someunit"); } ) or die qq {Could not fork for spork: $!};
      I can't turn  mycode.pl into a real module. Coz in real situation it is a executable binary.

        I don't know anything abour Acme::Spork, so I can't guess if it's the problem or not. But if you remove it from the equation, and use the system call normally, do you have the same problem? If removing it corrects the problem, I would contact the author for assistance

        Ok, actually the system call is run under Acme::Spork. Could it be the cause?
        Easiest, securest way to find if Acme::Spork (never heard of it) is the cause: take it out of the equation and test it!

        Just write:
        #!/usr/bin/perl -w use strict; my $uemail = 'foo@bar.com'; system("perl mycode.pl -email \"$uemail\"") or die "Well, probably NOT + a spork problem";

        or better yet, comment the spork line of your code and do a simple system call as above. Either way will tell you if this module is or is not the cause of your problem.

        <UPDATE> My apologies to HuckinFappy. I ended up answering the same thing...</UPDATE>

        Just my two cents.
Re: Problem passing a string in system function
by davorg (Chancellor) on May 19, 2006 at 10:30 UTC

    There doesn't seem to be any good reason why that wouldn't work.

    How are you accessing the arguments inside mycode.pl? Have you tried looking at the contents of @ARGV as soon as the program starts up?

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      I use Getop::Long to get the argument.
      I've looked at @ARGV, it gives:
      $VAR1 = [ '-type', 'sugar', '-unit', '6' ]; # where $sometype = 'foo'; $some_unit = 6;
      So as you can see EMAIL option and its value is totally unrecognize here.
      My overall construct under 'mycode.pl' looks like this:
      use strict; use Data::Dumper; use Getopt::Long; print Dumper \@ARGV ; my $type = 'bar'; my $no_unit = 1; my $help = 0; my $tg_email = ''; if ( @ARGV == 0 ) { &usage(); exit(1); } my $res = GetOptions ( "type=s"=>\$type, "unit=i"=>\$no_unit, "email=s"=>\$tg_email, "help"=>\$help,); if ($res) { print "$tg_email\n" # show nothing here. # and do some other thing }

        How strange! Running the following with no arguments

        #!/usr/bin/perl -l use strict; use warnings; use Data::Dumper; if (@ARGV) { print Dumper \@ARGV; } else { my $email = 'myemail@gmail.com'; system "$0 -email $email"; print '-' x 20; system $0, -email => $email; } __END__

        I get:

        $VAR1 = [ '-email', 'myemail@gmail.com' ]; -------------------- $VAR1 = [ '-email', 'myemail@gmail.com' ];
Re: Problem passing a string in system function
by halley (Prior) on May 19, 2006 at 19:28 UTC
    my $receipt_email = 'myemail@gmail.com"

    Mismatched quote types.

    You also didn't share what shell you're using. Single vs double-quotes (and other characters) have big differences on CMD.EXE vs bash vs other command line shells.

    --
    [ e d @ h a l l e y . c c ]

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://550460]
Approved by willyyam
Front-paged by neversaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-07-13 09:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.