Quick Perl question

Discussion in 'Perl Misc' started by MDS, Oct 20, 2003.

  1. MDS

    MDS Guest

    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.
    MDS, Oct 20, 2003
    #1
    1. Advertising

  2. MDS

    Anand Guest

    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";
    --

    Thanks in advance.
    --Anand


    Abigail wrote:
    > MDS () wrote on MMMDCCII September MCMXCIII in
    > <URL:news:>:
    > `' 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 & ?
    >
    >
    > Sure. Start your program with:
    >
    > (fork // die "Failed to fork: $!") && exit;
    >
    >
    > Abigail
    Anand, Oct 20, 2003
    #2
    1. Advertising

  3. Anand wrote:
    >
    > 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
    Michael P. Broida, Oct 20, 2003
    #3
  4. MDS

    Roy Johnson Guest

    Anand <> wrote in message news:<OcUkb.4571$>...
    > (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;
    Roy Johnson, Oct 21, 2003
    #4
  5. Abigail wrote:
    > Anand () wrote on MMMDCCII September MCMXCIII in
    > <URL:news:OcUkb.4571$>:
    > ** 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
    Chris Mattern, Oct 21, 2003
    #5
  6. Roy Johnson <> wrote:
    > Anand <> wrote in message news:<OcUkb.4571$>...
    > > (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;


    You actually need

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

    Your version exits in the child as well...

    --
    Steve
    Steve Grazzini, Oct 21, 2003
    #6
  7. MDS

    Mina Naguib Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    MDS wrote:
    > 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-----
    Mina Naguib, Oct 22, 2003
    #7
  8. MDS

    Roy Johnson Guest

    Steve Grazzini <> wrote in message news:<mmclb.9456$>...
    > Roy Johnson <> wrote:
    > > (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...


    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}
    Roy Johnson, Oct 23, 2003
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric J. Roode
    Replies:
    0
    Views:
    958
    Eric J. Roode
    Jul 19, 2003
  2. jack

    Quick perl question 2

    jack, Aug 25, 2003, in forum: Perl
    Replies:
    1
    Views:
    893
  3. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    Quick Question - Newby Question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Feb 14, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    636
    Iain Norman
    Feb 16, 2005
  4. JKop
    Replies:
    11
    Views:
    853
  5. Jürgen Exner

    Re: Quick perl script-regex question

    Jürgen Exner, Nov 4, 2011, in forum: Perl
    Replies:
    0
    Views:
    1,956
    Jürgen Exner
    Nov 4, 2011
Loading...

Share This Page