is there any command for catch in tcl in perl

Discussion in 'Perl Misc' started by vikram.varshney@gmail.com, Nov 21, 2007.

  1. Guest

    Hi
    I am Vikram Varshney. My script needs to catch whether a particular
    command fails or passes. This could be done very easily in tcl using
    "catch" which gives "1" output when the command fails and 0 as output
    when the command runs successfully. And what represents NULL character
    in perl. I have tried "\0" & "NULL" & / / & ' ' . But they are not
    working.

    You can answer me at :
    Thanks in advance

    Vikram Varshney
    , Nov 21, 2007
    #1
    1. Advertising

  2. wrote:
    > Hi
    > I am Vikram Varshney. My script needs to catch whether a particular
    > command fails or passes. This could be done very easily in tcl using
    > "catch" which gives "1" output when the command fails and 0 as output
    > when the command runs successfully. And what represents NULL character
    > in perl. I have tried "\0" & "NULL" & / / & ' ' . But they are not
    > working.


    perldoc -f eval

    > You can answer me at :

    (mailed and posted)
    --
    Mails please to josef dot moellers
    and I'm on gmx dot de.
    Josef Moellers, Nov 21, 2007
    #2
    1. Advertising

  3. (mailed and posted)

    wrote:
    > Hi
    > I am Vikram Varshney. My script needs to catch whether a particular
    > command fails or passes. This could be done very easily in tcl using
    > "catch" which gives "1" output when the command fails and 0 as output
    > when the command runs successfully. And what represents NULL character
    > in perl. I have tried "\0" & "NULL" & / / & ' ' . But they are not
    > working.


    Sorry, I missed this last part.

    The ASCII NUL character (note the single 'L'! "NULL" is a special
    pointer value in various programming languages, e.g. C, C++) is "\0",
    e.g. you can (sort-of) pretty-print Linux' /proc/.../cmdline entry by

    open(my $ppc, '<', '/proc/self/cmdline') or die "Cannot open cmdline: $!";
    my $commandline = <$ppc>;
    $commandline =~ s/\0/ /g;
    print "$commandline\n";

    If you mean the "0" as returned by tcl's "catch"-command, then note that
    Perl's "eval" will return "undef" (and $@ is set to the error message),
    which you can simply check with e.g.

    if (eval($command)) {
    print "Command was successfull!!\n";
    } else {
    print "Command failed: $@\n";
    }

    Otherwise, a "0" is a 0.

    HTH,
    --
    Mails please to josef dot moellers
    and I'm on gmx dot de.
    Josef Moellers, Nov 21, 2007
    #3
  4. Ben Morrow Guest

    Quoth Josef Moellers <>:
    > wrote:
    > >
    > > I am Vikram Varshney. My script needs to catch whether a particular
    > > command fails or passes. This could be done very easily in tcl using
    > > "catch" which gives "1" output when the command fails and 0 as output
    > > when the command runs successfully. And what represents NULL character
    > > in perl. I have tried "\0" & "NULL" & / / & ' ' . But they are not
    > > working.


    "\0" is a string containing a single ASCII nul character.
    "NULL" is a string containing four characters 'N', 'U', 'L', 'L'.
    / / is a regex that matches a single space.
    ' ' is a string containing a single space.

    These are all different, and in Perl all are true values. Perl has three
    false values: the empty string '', the number 0 (or the string '0' which
    converts to 0), and the undefined value undef.

    > The ASCII NUL character (note the single 'L'! "NULL" is a special
    > pointer value in various programming languages, e.g. C, C++) is "\0",
    > e.g. you can (sort-of) pretty-print Linux' /proc/.../cmdline entry by
    >
    > open(my $ppc, '<', '/proc/self/cmdline') or die "Cannot open cmdline: $!";
    > my $commandline = <$ppc>;
    > $commandline =~ s/\0/ /g;
    > print "$commandline\n";
    >
    > If you mean the "0" as returned by tcl's "catch"-command, then note that
    > Perl's "eval" will return "undef"


    Note: Josef doesn't literally mean "undef" (a string), but undef, which
    is a special Perl value a little like the NULL pointer in C or NULL in
    SQL (but not entirely like either :) ).

    > (and $@ is set to the error message),
    > which you can simply check with e.g.
    >
    > if (eval($command)) {
    > print "Command was successfull!!\n";
    > } else {
    > print "Command failed: $@\n";
    > }


    Safer would be

    if (defined eval $command) {

    which allows for the $command to return some false-but-defined value
    like 0 or ''. Note also that you can use eval {} (see perldoc -f eval)
    to avoid the security and speed penalties of eval "".

    Ben
    Ben Morrow, Nov 21, 2007
    #4
  5. Ben Morrow wrote:
    > Quoth Josef Moellers <>:


    >> If you mean the "0" as returned by tcl's "catch"-command, then note that
    >> Perl's "eval" will return "undef"

    >
    > Note: Josef doesn't literally mean "undef" (a string), but undef, which
    > is a special Perl value a little like the NULL pointer in C or NULL in
    > SQL (but not entirely like either :) ).


    ACK

    To the OP (and maybe others): this is one of the reasons why "You can
    answer me at : user@host" is A Bad Idea(tm): whoever sends you an answer
    may have gotten it wrong or not quite right or a better solution may
    exist. Replying to the newsgroup gives other people the possibility to
    comment on replies.
    Another reason is that others (like myself) can learn from the replies
    (and the corrections of them ;-)
    --
    Mails please to josef dot moellers
    and I'm on gmx dot de.
    Josef Moellers, Nov 22, 2007
    #5
  6. Guest

    On Nov 21, 8:18 pm, Glenn Jackman <> wrote:
    > At 2007-11-21 09:54AM, "" wrote:
    >
    > > Hi
    > > I amVikramVarshney. My script needs to catch whether a particular
    > > command fails or passes. This could be done very easily in tcl using
    > > "catch" which gives "1" output when the command fails and 0 as output
    > > when the command runs successfully.

    >
    > perldoc -f eval
    >
    > If you're building some code dynamically into a string:
    >
    > eval "$some_code";
    > if ($@) {
    > # some error condition
    > }
    >
    > Otherwise:
    >
    > eval { some(code()); };
    > handle_error($@) if $@;
    >
    > > And what represents NULL character
    > > in perl. I have tried "\0" & "NULL" & / / & ' ' . But they are not
    > > working.

    >
    > In what context do you need a null?
    > my $null = 0;
    >
    > > You can answer me at :

    >
    > Um, no.
    >
    > --
    > Glenn Jackman
    > "You can only be young once. But you can always be immature." -- Dave Barry


    actully i need to search something from a big database. So , if
    someone gives a wrong code then the output is blank (no space). I
    needed "NULL" for that purpose. I tried "length($var ==0)" but its
    very unstable and gives shaky results everytime.
    , Nov 23, 2007
    #6
    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. Peter Gordon
    Replies:
    2
    Views:
    322
    Peter Hansen
    Nov 14, 2004
  2. Allen
    Replies:
    3
    Views:
    301
    Gabriel Genellina
    Jun 14, 2007
  3. Royan
    Replies:
    11
    Views:
    1,028
    Arne Vajhøj
    Oct 25, 2008
  4. Mumia W.

    Inline::Tcl vs. Inline::Tcl

    Mumia W., Aug 23, 2006, in forum: Perl Misc
    Replies:
    0
    Views:
    137
    Mumia W.
    Aug 23, 2006
  5. Anil A Kumar
    Replies:
    2
    Views:
    127
    Jürgen Exner
    Aug 3, 2011
Loading...

Share This Page