is there any command for catch in tcl in perl

V

vikram.varshney

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 : (e-mail address removed)
Thanks in advance

Vikram Varshney
 
J

Josef Moellers

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 : (e-mail address removed)
(mailed and posted)
 
J

Josef Moellers

(mailed and posted)

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,
 
B

Ben Morrow

"\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
 
J

Josef Moellers

Ben said:
Quoth Josef Moellers <[email protected]>:

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 ;-)
 
V

vikram.varshney

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 $@;


In what context do you need a null?
my $null = 0;


Um, no.

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.
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top