is there any command for catch in tcl in perl
is there any command for catch in tcl in perl
am 21.11.2007 15:54:24 von 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 : vikram.varshney@gmail.com
Thanks in advance
Vikram Varshney
Re: is there any command for catch in tcl in perl
am 21.11.2007 16:15:58 von Josef Moellers
vikram.varshney@gmail.com 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 : vikram.varshney@gmail.com
(mailed and posted)
--
Mails please to josef dot moellers
and I'm on gmx dot de.
Re: is there any command for catch in tcl in perl
am 21.11.2007 16:18:04 von Glenn Jackman
At 2007-11-21 09:54AM, "vikram.varshney@gmail.com" 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.
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 : vikram.varshney@gmail.com
Um, no.
--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
Re: is there any command for catch in tcl in perl
am 21.11.2007 17:40:34 von Josef Moellers
(mailed and posted)
vikram.varshney@gmail.com 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.
Re: is there any command for catch in tcl in perl
am 21.11.2007 18:40:47 von Ben Morrow
Quoth Josef Moellers <5502109103600001@t-online.de>:
> vikram.varshney@gmail.com 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
Re: is there any command for catch in tcl in perl
am 22.11.2007 15:48:18 von Josef Moellers
Ben Morrow wrote:
> Quoth Josef Moellers <5502109103600001@t-online.de>:
>> 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.
Re: is there any command for catch in tcl in perl
am 23.11.2007 07:39:23 von vikram.varshney
On Nov 21, 8:18 pm, Glenn Jackman wrote:
> At 2007-11-21 09:54AM, "vikram.varsh...@gmail.com" 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 :vikram.varsh...@gmail.com
>
> 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.
Re: is there any command for catch in tcl in perl
am 23.11.2007 17:19:50 von Glenn Jackman
At 2007-11-23 01:39AM, "vikram.varshney@gmail.com" wrote:
> 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.
You'd probably want to count the number of rows returned instead. Are
you using DBI?
I hope you don't actually have "length($var ==0)" -- that will probably
always return true. Do you "use warnings"?
--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry