Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Add Perl Varibles in system()

by John M. Dlugosz (Monsignor)
on May 11, 2009 at 21:33 UTC ( [id://763359]=note: print w/replies, xml ) Need Help??


in reply to Add Perl Varibles in system()

You probably meant
system (qq(nmap "$target"));
rather than using the "indirect object" form.

Also, your line my ($target); should be my $target;. Don't add extra parens because they are pretty to you—that will bite you later because that is not the actual syntax. You are specifying list context there. Normally when list context is needed you might say (my $s)=func_returning_list();, with the my on the inside. Note that you would not say my ($x,$y) as that is actually a syntax error.

—John

Replies are listed 'Best First'.
Re^2: Add Perl Varibles in system()
by FunkyMonk (Chancellor) on May 11, 2009 at 22:25 UTC
    Normally when list context is needed you might say (my $s)=func_returning_list();, with the my on the inside
    my ($s) causes list context too.
    Note that you would not say my ($x,$y) as that is actually a syntax error.
    That works fine here on perl 5.10.0 and on any earlier perl I can remember

    $ perl -Mstrict -cwe 'my ($x,$y)' -e syntax OK
      I must be getting old. I distinctly remember learning that lesson when Perl 5 was freshly minted. The grammar must have been "improved" since then. You are right about the latter for sure; how many times do I say
      my ($x,$y,$z)= @_; </code? So the <code>my ($x)
      vs (my $x) must be (or have been) something more subtle.
      I verified that it works as far back as 5.6.0, but I'm sure it worked even before that.
Re^2: Add Perl Varibles in system()
by ikegami (Patriarch) on May 11, 2009 at 23:04 UTC

    Normally when list context is needed you might say (my $s)=func_returning_list();, with the my on the inside.

    Based on all the posts I've seen here, that's actually much rarer than my ($s). In fact, I rarely see (my $s). It's just too wordy. Compare

    (my $x, my $y, my $z) = @_;
    with
    my ($x, $y, $z) = @_;

    I was going to count the nodes with "(my" and count those with "my (", but I don't have a handy way of avoiding open(my $fh, ...). Despite that common construct seriously throwing off the count, "my (" still appears more often.

    And of course, your code is subject to the shell equivalent of Bobby Tables.

      Thank you for your quick response. And the reason why  $target was written the way it was is because it was orginally going to be mutiple varibles, and I forgot to remove the paraentheses when I took ou the other varible. But now I have a different problem I want to the success of the program. This is what I believe will work:
      system( 'nmap', $ver, $os, $target ' -oG pentest.txt')=0 || print "Nma +p failed! $!"; exit1;
      But there is probably a better way to write that. Any suggestions?

        Bugs:

        • Missing comma
        • "-oG" and "pentest.txt" are separate args. You can't pass them as one.
        • The dash should be the start of the arg, not a space.

        You also introduced four bugs in the error handling!

        Fixed:

        system( 'nmap', $ver, $os, $target, '-oG', 'pentest.txt' ) or die "nmap failed" $!/$?\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://763359]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-24 09:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found