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

Re^2: I think I just found a good reason to use backticks in a void context.

by OfficeLinebacker (Chaplain)
on Jan 14, 2007 at 01:36 UTC ( [id://594586]=note: print w/replies, xml ) Need Help??


in reply to Re: I think I just found a good reason to use backticks in a void context.
in thread I thought I found a good reason to use backticks in a void context, but I was wrong.

OK, I don't understand what you mean by "fair." I found a way to do something that's faster than another way of doing it. I don't get how making something faster that works is somehow perceived as a bad thing. You say

The reduction in time is not because of backticks, but because you're avoiding the shell metachars, meaning that Perl can go directly to the command, rather than forking the shell first.
As far as I can tell, using backticks is precisely what allows me to avoid using shell metacharacters (I am assuming you mean > and &). I guess I am having trouble understanding your position.

Do you have a better alternative?

I like computer programming because it's like Legos for the mind.
  • Comment on Re^2: I think I just found a good reason to use backticks in a void context.

Replies are listed 'Best First'.
Re^3: I think I just found a good reason to use backticks in a void context.
by merlyn (Sage) on Jan 14, 2007 at 02:50 UTC
    Right... one that's more fair would be to fork yourself and close STDOUT and STDERR, which would probably be even faster. Something like:
    defined(my $kidpid = fork) or die "cannot fork: $!"; unless ($kidpid) { close STDOUT; close STDERR; exec "your", "multiarg", "command", "here"; die "$!"; } waitpid($kidpid, 0);

    That'd be the equivalent to your backtick-that-just-happens-to-not-invoke-the-shell, but probably even more efficient.

Re^3: I think I just found a good reason to use backticks in a void context.
by Aristotle (Chancellor) on Jan 14, 2007 at 02:28 UTC

    Using backticks is not the only way of avoiding the shell. Pass a list to system (or to a three-arg pipe-open for convenient output suppression) and you get the same effect.

    So no, this isn’t a reason to use backticks in void context; it is merely a reason to avoid invoking the shell when you don’t need it. Even if backticks were required for that, saving a couple of microseconds here and there wouldn’t constitute a good reason to do it, as per the topic.

    Makeshifts last the longest.

Re^3: I think I just found a good reason to use backticks in a void context.
by Errto (Vicar) on Jan 14, 2007 at 02:01 UTC
    I think merlyn's point is that in the OP, you seem to be claiming that it is using backticks itself which makes the progam faster, whereas he is pointing out that the actual cause is the lack of metacharacters. So if you were to use system instead of backticks, but still without redirection or other metacharacters, you would get the same benefit. Therefore it's not really fair to claim that the backticks made the difference.

      I agree that if I had used system(), only with no redirection, the performance would probably be the same. I didn't try that, but I did try backticks WITH redirection and got a similar penalty (or loss of efficiency?) to using system() with redirection. I noted that in an update to the OP. I am sorry if I was unfair.

      Tell me, is the following an accurate characterization:

      By using backticks, I am effectively making the inside of my program the system call's STDOUT. Using backticks in a void context is the functional equivalent of redirecting output of the system call to /dev/null. With backticks, the computer has to do less work (and the programmer has to do less typing).

      I don't mean to endorse of the practice by the above statements.


      I like computer programming because it's like Legos for the mind.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-25 06:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found