Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
go ahead... be a heretic
 
PerlMonks  

Re^2: Strange system call failure in OS X

by afoken (Parson)
on Feb 16, 2013 at 17:15 UTC ( #1019047=note: print w/ replies, xml ) Need Help??


in reply to Re: Strange system call failure in OS X
in thread Strange system call failure in OS X

eval { $result = qx(scutil --set LocalHostName $name) };
  • qx// is the generic form of `...`
  • `...` passes a single string to the shell (except when perl is sure that a shell is not needed), so you have to handle an unknown shell with unknown behavior. Quoting is necessary to prevent shell injection, but quoting rules differ from shell to shell. See http://www.in-ulm.de/~mascheck/various/ to get an idea how different shells can behave.
  • `...` does not die, eval won't catch any error in external programs, so eval is useless here.

Read the "Safe Pipe Opens" section of perlipc for a secure replacement for `...`.

The shell problem is also present in the single argument versions of system and exec, so make sure to use the multi-argument versions, which don't have this problem.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)


Comment on Re^2: Strange system call failure in OS X
Select or Download Code
Re^3: Strange system call failure in OS X
by karlgoethebier (Deacon) on Feb 16, 2013 at 19:19 UTC

    Mmh...?

    #!/usr/bin/env perl + eval { qx(nose --cuke) || die qq(shit: $!\n) }; print qq(Trapped this $@) if $@; __END__ Karls-Mac-mini:monks karl$ ./nose.pl Trapped this shit: No such file or directory

    Update: I always thought that it isn't a bad idea to wrap a call to a subshell into a eval block. And i don't know what is generic with qx. It's just the same as backticks, isn't it? Just a subshell.

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (11)
As of 2014-04-18 14:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (469 votes), past polls