Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Strange system call failure in OS X

by karlgoethebier (Monsignor)
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»

Replies are listed 'Best First'.
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$ ./ 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»

Re^2: Strange system call failure in OS X
by afoken (Abbot) 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 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.


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


      #!/usr/bin/env perl + eval { qx(nose --cuke) || die qq(shit: $!\n) }; print qq(Trapped this $@) if $@; __END__ Karls-Mac-mini:monks karl$ ./ 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1019038]
[marto]: the price for cheap tablets that I can actually put an open ROM on, compared to the price for a cheap Chinese intel tablet I could wipe and put Linux on
[Corion]: marto: Ah, yeah. If you can actually put Linux on them. I'm somewhat wary of the new Win10 Atom tablet thingies that are sold for ~EUR 280, and whether you can enter their UEFI at all to install Linux there. But that's sure worth a try.

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2018-05-23 11:04 GMT
Find Nodes?
    Voting Booth?