• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Capturing command output without invoking the system shell: potential pitfalls?

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Perl you can do this:



By reading from STDOUT (or STDERR) you can look at the output of external commands, without running them through the shell via backticks.

The thing is, I have never seen this recommended anywhere for anything. Are there hazards associated with doing this? If so, what are they? Is there a better way of e.g. setting up pipelines in Perl (or other scripting languages) that does *not* rely on the system command shell?

(Also, is the above portable to Windows?)
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the closest I could get in Windows:



When you call system, you are still calling an external command. I'm not sure what you get with that syntax that you don't get with the above code.
 
Daniel Levine
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You get command invocation *without using the shell* with somewhat less cumbersome syntax. When you invoke system() on a list rather than a string, it bypasses the shell when running the external command.

Though you can do the same with open in current Perl versions, the syntax is just a bit clunky:



Or something like that. Cool to hear opening pipelines works on Windows anyway.
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't do that in Windows. You get a List form of pipe open not implemented error. But then if you're doing external calls, it's not really portable anyway.
 
permaculture is giving a gift to your future self. After reading this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic