Quick Perl question

M

MDS

Hi there,

Is there an easy way to write a Perl script for UNIX that returns the
user to the command line on execution, other than using ./myscript.pl
2>&1 or ./myscript.pl & ?

Thanks in advance,

Marc.
 
A

Anand

I tried this but doesn't work. Could you please tell me where is glitch.
--
#!/usr/bin/perl


(fork() || die "Cannot fork: $!") && exit;



$num = "12345";
$res = substr ($num, 0, 3);
print "$res\n";
 
M

Michael P. Broida

Anand said:
I tried this but doesn't work. Could you please tell me where is glitch.
--
#!/usr/bin/perl

(fork() || die "Cannot fork: $!") && exit;

$num = "12345";
$res = substr ($num, 0, 3);
print "$res\n";
--

1) How "doesn't work"??? What DOES it do? Error messages?
Does it get to the "print" statement? Did you use
"use strict" and "use warnings"?

2) You didn't do what Abigail told you to do. (I don't know if
Abigail was right or wrong, but ... <grin>) You used
parens on the "fork" statement; Abigail did not. Does
it work without the parens?? Abigail also used "//" where
you have "||"; I don't know if that was a typo or not,
but I suspect it was.

3) Top posting doesn't bother me, but many of the real gurus
here dislike it enough to not help topposters. So don't
top post if you want to get quality (better than mine
<grin>) help.

Mike
 
R

Roy Johnson

Anand said:
(fork() || die "Cannot fork: $!") && exit;

Abigail was using the // operator, which is available as an extension,
but will be part of the core distribution in 5.10.0

You are using the ordinary || operator. The difference is that //
checks for definedness, not truth. To get that behavior, you need:
(defined(fork()) || die "Cannot fork: $!") && exit;
 
C

Chris Mattern

Abigail said:
Anand ([email protected]) wrote on MMMDCCII September MCMXCIII in
<URL:** I tried this but doesn't work. Could you please tell me where is glitch.

Not if you top post.
And not if you can't say a) what you expected it to do and b) what
it *did* do. "It doesn't work" is not an adequate description of
your problem.

Chris Mattern
 
S

Steve Grazzini

Roy Johnson said:
Abigail was using the // operator, which is available as an extension,
but will be part of the core distribution in 5.10.0

You are using the ordinary || operator. The difference is that //
checks for definedness, not truth. To get that behavior, you need:
(defined(fork()) || die "Cannot fork: $!") && exit;

You actually need

defined (my $pid = fork) or die "Cannot fork: $!";
exit if $pid;

Your version exits in the child as well...
 
M

Mina Naguib

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi there,

Is there an easy way to write a Perl script for UNIX that returns the
user to the command line on execution, other than using ./myscript.pl
2>&1 or ./myscript.pl & ?

The easiest way is:
use Proc::Daemon;
Proc::Daemon::Init;

The manual way is to do everything manually that the above module's documentation says to do, see:
perldoc -f fork

You can usually get away with just:
fork && exit;

But you're better off using the very first approach.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQE/llineS99pGMif6wRAtLMAKCV9/GBS2nDYStHCt8EbFvAfKV2cgCgmUjH
BGsExklcv5ahikcEU/Y56D4=
=pJHR
-----END PGP SIGNATURE-----
 
R

Roy Johnson

Steve Grazzini said:
You actually need

defined (my $pid = fork) or die "Cannot fork: $!";
exit if $pid;

Your version exits in the child as well...

Oops. Should have had:
(defined(fork() && exit)) or die "Cannot fork: $!\n";
for properly obscure, though correct, behavior. :) I do tend to
forget that // returns the left side if it is defined, even if it is
false.

Actually, the correct translation of a//b is defined(a) ? a : b, so I
should have said
my $pid;
(defined($pid=fork()) ? $pid : die "Cannot fork: $!") && exit;

Here's another interesting way to approximate // behavior (in the
sense that the expressions look fairly similar):
${dref(fork()) || die "Cannot fork: $!"} && exit;

# Return a ref to the value, if it is defined.
sub dref {
(defined $_[0]) ? \$_[0] : undef;
}

But for something like
$a // $b // $c
you would need to be sure you returned references in each case:
${dref($a) || $dref($b) || \$c}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top