Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Strange system call failure in OS X

by karlgoethebier (Priest)
on Feb 16, 2013 at 13:38 UTC ( #1019038=note: print w/ replies, xml ) Need Help??


in reply to Strange system call failure in OS X

BTW, why don't you do it like this:

#!/usr/bin/env perl use strict; use warnings; my $name = qq(3c0754131c6d); my $result; eval { $result = qx(scutil --set LocalHostName $name) };

..and then take a look $result, $?>>8 and $@?

Just an idea, don't know if this helps but best regards, Karl

«The Crux of the Biscuit is the Apostrophe»


Comment on Re: Strange system call failure in OS X
Select or Download Code
Re^2: Strange system call failure in OS X
by afoken (Parson) on Feb 16, 2013 at 17:15 UTC
    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". ;-)

      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»

Re^2: Strange system call failure in OS X
by endor-moon (Initiate) on Feb 18, 2013 at 15:12 UTC

    Actually, this is the way I ended up doing these system calls due to its relative simplicity:

    # Declare dependencies use File::Slurp; # Declare variables we'll be using my $name = ""; my @args = (); my $result = ""; # Set computer name based on MAC address of en0 my @entries = qx(ifconfig en0 | awk '/ether/ { gsub(":", ""); print \$ +2 }'); chomp(@entries); $name = $entries[0];

      Hi endor-moon and welcome,

      After bothering myself a bit with awk (i repressed the details) i decided to take the swiss katana. I'm assuming that you just want the mac address:

      #!/usr/bin/env perl + use strict; use warnings; my $mac = ( split " ", ( qx(ifconfig en0) )[2] )[1]; print qq($mac\n); __END__ Karls-Mac-mini:monks karl$ ./endor-moon.pl 2c:07:54:5e:3e:f0 # i faked this, really ;-)

      This works for me. Please note also that your code didn't output anything (for me).

      Btw, why do you load a module that you don't use (File::Slurp) as well as some variables (@args, $result)...or do i miss something?

      My best 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://1019038]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2014-12-22 09:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (113 votes), past polls