[comp.lang.perl] Two backtick questions

chrise@hpnmdla.HP.COM (Chris Eich) (06/15/90)

Two questions about `` (backticks):

1.  Is there an easier way than:

	@array = split(/\n/, `command`);

    to fill an array with each line of `command`?  (I'm not complaining,
    as this is pretty easy; just curious.)

2.  How can I catch the stderr of `command`?  In the shell, I can do:

    result=`command 2>&1`

    I can't see how to do it in perl.

Chris

chrise@hpnmdla.HP.COM (Chris Eich) (06/15/90)

Let me amend my two questions:

    1.  Is there an easier way than:

    	@array = split(/\n/, `command`);

        to fill an array with each line of `command`?  (I'm not complaining,
        as this is pretty easy; just curious.)

I guess I'd like `command` to return an array of lines when in an array
context, just as <FILE> does.

    2.  How can I catch the stderr of `command`?  In the shell, I can do:

        result=`command 2>&1`

        I can't see how to do it in perl.

Never mind!!!  I left a newline on a variable I put at the end of
"command", causing the redirection to be lost.

Chris

tneff@bfmny0.BFM.COM (Tom Neff) (06/15/90)

>| 2.  How can I catch the stderr of `command`?  In the shell, I can do:
>|     result=`command 2>&1`

>You just did it!
>
>You just can't get both stdout and stderr in *separate* places, unless
>you muss with fork() and pipe() yourself.

Wellllll...

	tmpf = "/tmp/xx.$$";
	result = `command 2>$tmpf`;
	reserr = `cat $tmpf; rm -f $tmpf`;

-- 
Annex Canada now!  We need the room,    \)      Tom Neff
    and who's going to stop us.         (\      tneff@bfmny0.BFM.COM

merlyn@iwarp.intel.com (Randal Schwartz) (06/15/90)

In article <8310004@hpnmdla.HP.COM>, chrise@hpnmdla (Chris Eich) writes:
| 1.  Is there an easier way than:
| 
| 	@array = split(/\n/, `command`);
| 
|     to fill an array with each line of `command`?  (I'm not complaining,
|     as this is pretty easy; just curious.)

That's what I've been using.  With my luck, Larry will probably post
something that does it in half-a-character, or something. :-)

| 2.  How can I catch the stderr of `command`?  In the shell, I can do:
| 
|     result=`command 2>&1`
| 
|     I can't see how to do it in perl.

You just did it!

	$result = `perl -e 'print STDERR "hello world\n"' 2>&1`;
	print "<$result>";

You just can't get both stdout and stderr in *separate* places, unless
you muss with fork() and pipe() yourself.

print `perl -e 'print STDERR "Just another Perl hacker,"' 2>&1`
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/

merlyn@iwarp.intel.com (Randal Schwartz) (06/15/90)

In article <15592@bfmny0.BFM.COM>, tneff@bfmny0 (Tom Neff) writes:
| >You just can't get both stdout and stderr in *separate* places, unless
| >you muss with fork() and pipe() yourself.
| 
| Wellllll...
| 
| 	tmpf = "/tmp/xx.$$";
| 	result = `command 2>$tmpf`;
| 	reserr = `cat $tmpf; rm -f $tmpf`;

Hey, a tempfile is just a pipe with an attitude and a strong will to live...

:-)
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/

dglo@ADS.COM (Dave Glowacki) (06/16/90)

In article <1990Jun15.013714.24031@iwarp.intel.com> merlyn@iwarp.intel.com (Randal Schwartz) writes:
>In article <8310004@hpnmdla.HP.COM>, chrise@hpnmdla (Chris Eich) writes:
>| 2.  How can I catch the stderr of `command`?  In the shell, I can do:
>| 
>|     result=`command 2>&1`
>| 
>|     I can't see how to do it in perl.
>
>You just did it!
>
>	$result = `perl -e 'print STDERR "hello world\n"' 2>&1`;
>	print "<$result>";

Is this guaranteed, even if the user's shell (or SHELL environment variable)
is set to /bin/csh?

If so, is this also true for pipes-as-filehandles (as in:
	open(SOMETHING, "|program >logfile 2>&1");
or something like that?
--
Dave Glowacki          dglo@ads.com          Advanced Decision Systems

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (06/17/90)

In article <72A$+6+@ads.com> dglo@ADS.COM (Dave Glowacki) writes:
[old stuff deleted]
: >	$result = `perl -e 'print STDERR "hello world\n"' 2>&1`;
: >	print "<$result>";
: 
: Is this guaranteed, even if the user's shell (or SHELL environment variable)
: is set to /bin/csh?
: 
: If so, is this also true for pipes-as-filehandles (as in:
: 	open(SOMETHING, "|program >logfile 2>&1");
: or something like that?

Yes and yes.  The internal routine do_exec() explicity calls /bin/sh.
This is how most popen()s and system()s work.  The SHELL environment variable
should mostly be reserved for specifying the desired *interactive* shell,
in my opinion.  Otherwise it only obfuscates portability.

If you want to execute a command using /bin/csh, invoke /bin/csh directly.

Larry