From: Malcolm B. <mal...@gm...> - 2006-03-31 23:26:14
|
Jan-Benedict Glaw wrote: > On Fri, 2006-03-31 23:17:00 +0100, Malcolm Box <mal...@gm...> wrote: > >>$output = `command`; >>if($? >> 8) {die "Problem with command";} >> >>if you want to get rid of the \n on the string, then do: >> >>chomp $output; > > > Is there a way so supply the arguments one-by-one? I'd like to get > things like whitespace in a path correct, thus I guess something like > exec() vs. system() may be needed? You can use system() with a list of parameters, which will then preserve whitespace in pathnames (evil though this is). e.g. system("git-command", "param1", "path with spaces in it") This doesn't capture the output though, so you might have to fork and read the output. There's a good example of how to do this in the perlsec man page - look for "backtick". Doing things that way would be a good idea if the command is ever run in response to untrusted user input - i.e. if it can be triggered by the browser interface, since there would then be a potential injection attack. The other thing you could try is quoting the pathnames in the backticks - from some simple experiments this would appear to work. > > >>> * Given that a program outputs numerous lines of which the first >>> lines have a strict syntax (and the following are free-form text), >>> what's the easiest way to get one of the lines with known format >>$output = `git-cat-file commit...`; >>if ($? >> 8) { die "Something interesting happened";}; >> >>($author_line) = ($output=~m/^(author .*)$/m; >>($author_email) = ($author_line=~/<(.*)>/; >> >>or something like that should do the trick. The basic flow is to read >>everything into the $output variable, then use the regexp operators of >>perl to chop out what you need. > > > Is the unmatching number of opening vs. closing braces intended > (right-hand)? No, sorry! Comes of posting code that I've not run. It should be: ($author_line) = ($output=~m/^(author .*)$/m); >>The first regexp uses the /m modifier so that it will deal with a >>multi-line string in $output. The second doesn't need this since we've > > > Does this take care of throwing away all but the first author line? The $author_line will contain only the first line starting with author. So it does throw away all the rest. >>just chopped out one line. Both regexps use the fact that in list >>context a match (m//) operation returns a list of all matched bracketed >>expressions. Cheers, Malcolm |