Is there throw/try/catch semantics in Perl? 
Author Message
 Is there throw/try/catch semantics in Perl?

Hi

I am new to Perl but not to C++/Java.

I am looking for a mechanism similar to throw and try/catch semantics in C++
and Java.

How do I do this in Perl?

-TIA
David



Tue, 02 Sep 2003 11:24:09 GMT  
 Is there throw/try/catch semantics in Perl?

: Hi

: I am new to Perl but not to C++/Java.

: I am looking for a mechanism similar to throw and try/catch semantics in C++
: and Java.

: How do I do this in Perl?


e.g.

        eval { # TRY to run the following lines of code
               #
               open FILE , ">$file" or die "open $file: $!";

               close FILE           or die "close: $!";
             } ;

        # now "catch" any errors



        }

        else    
        { # there were no errors, the TRY code ran to completion
          # you may not have any need of the else
        }

There are ways to name the blocks of code so they can be called TRY and
CATCH if you want, and other rearrangements of the code to make it look
more like another language, etc.  Previous posts have shown some nifty
ways to do all that.

--
Want to access the command line of your CGI account?  Need to debug your
installed CGI scripts?  Transfer and edit files right from your browser?

What you need is "ispy.cgi" - visit http://nisoftware.com/ispy.cgi



Wed, 03 Sep 2003 08:13:40 GMT  
 Is there throw/try/catch semantics in Perl?

Quote:

>Hi

>I am new to Perl but not to C++/Java.

>I am looking for a mechanism similar to throw and try/catch semantics in
C++
>and Java.

>How do I do this in Perl?

eval {

        ..... or die "Exception";

Quote:
};


{
    .....

Quote:
}


John

--
Castle Amber - Software Development
Perl / Java / C / C++ developer *available*

or visit http://castleamber.co.nz/



Wed, 03 Sep 2003 08:58:35 GMT  
 Is there throw/try/catch semantics in Perl?


Quote:

>: Hi

>: I am new to Perl but not to C++/Java.

>: I am looking for a mechanism similar to throw and try/catch semantics in C++
>: and Java.

>: How do I do this in Perl?


>There are ways to name the blocks of code so they can be called TRY and
>CATCH if you want, and other rearrangements of the code to make it look
>more like another language, etc.  Previous posts have shown some nifty
>ways to do all that.

IMHO the niftiest is the Error.pm module from Graham Barr, on CPAN.

There should be something even niftier yet in Perl 6.

--
Peter Scott



Wed, 03 Sep 2003 23:35:46 GMT  
 Is there throw/try/catch semantics in Perl?

Quote:
> eval {

>         ..... or die "Exception";
> };

> {
>     .....
> }


"Exception at blug.pl line 37", so that the following 'if' test will
fail.  You probably want


or something else.



Thu, 04 Sep 2003 19:51:50 GMT  
 Is there throw/try/catch semantics in Perl?

Quote:
> >I am looking for a mechanism similar to throw and try/catch semantics in
C++ and Java.
> >How do I do this in Perl?
> eval {
>         ..... or die "Exception";
> };

> {
>     .....
> }



My apologies for not being sufficiently clear in my initial inquiry.


Perl books).

However, I have picked up the distinct impression that 'eval' invokes the
full Perl compiler at run time (am I correct?).

This is not-so-subtly different than having the 'eval' statement compiled
along with the rest of the code, especially when heavy looping is involved.
Thus the 'eval' semantics are not quite the same as C++'s or Java's (again,
especially in light of heavy looping). In the cases that I am currently
programming, the 'eval' statements are known at compile time (not compiling
statements inside variables/expressions).

-TIA
David



Fri, 05 Sep 2003 06:34:15 GMT  
 Is there throw/try/catch semantics in Perl?

: > eval {
: >
: >         ..... or die "Exception";
: > };

: > {
: >     .....
: > }


: "Exception at blug.pl line 37", so that the following 'if' test will
: fail.  You probably want


: or something else.


die was called.


that is has any value at all.






        do not think so, but perhaps someone could confirm this. In

--
Want to access the command line of your CGI account?  Need to debug your
installed CGI scripts?  Transfer and edit files right from your browser?

What you need is "ispy.cgi" - visit http://nisoftware.com/ispy.cgi



Fri, 05 Sep 2003 11:20:10 GMT  
 Is there throw/try/catch semantics in Perl?

Quote:
>However, I have picked up the distinct impression that 'eval' invokes the
>full Perl compiler at run time (am I correct?).

Yes you are correct, if the argument to eval is a scalar value.
However, if the argument to eval is a code block, it is only parsed once
(at compile time, like your other code).

See "perldoc -f eval" for more information.
--

Systems and Network Administrator - HiWAAY Internet Services
I don't speak for anybody but myself - that's enough trouble.



Fri, 05 Sep 2003 12:03:57 GMT  
 Is there throw/try/catch semantics in Perl?

: >However, I have picked up the distinct impression that 'eval' invokes the
: >full Perl compiler at run time (am I correct?).

: Yes you are correct, if the argument to eval is a scalar value.
: However, if the argument to eval is a code block, it is only parsed once
: (at compile time, like your other code).

Plus, the "full Perl compiler" is always invoked for the script anyways,
so unless you are needlessly eval'ing a literal string in a loop then it
doesn't make any difference - except for the time at which certain errors
can be detected.

--
Want to access the command line of your CGI account?  Need to debug your
installed CGI scripts?  Transfer and edit files right from your browser?

What you need is "ispy.cgi" - visit http://nisoftware.com/ispy.cgi



Sat, 06 Sep 2003 02:02:39 GMT  
 Is there throw/try/catch semantics in Perl?

Quote:
> : >However, I have picked up the distinct impression that 'eval' invokes
the
> : >full Perl compiler at run time (am I correct?).

> Plus, the "full Perl compiler" is always invoked for the script anyways

[...],

As I noted in an earlier posting, my current need is focused along the lines
of 'eval BLOCK'. Based on the other feedback here it is clear that such a
construct is compiled just once (which is what I was hoping for since I will
ultimately convert the script into a stand-alone executable).

Given your statement that the full Perl Compiler is always invoked for the
script, I am forced to ask what happens when a script containing eval BLOCK
is converted into a stand-alone executable (say  a .exe on Windows 2K). Is
the full compiler always invoked as well at run time?

-David



Sat, 06 Sep 2003 10:21:51 GMT  
 Is there throw/try/catch semantics in Perl?

David> Given your statement that the full Perl Compiler is always
David> invoked for the script, I am forced to ask what happens when a
David> script containing eval BLOCK is converted into a stand-alone
David> executable (say a .exe on Windows 2K). Is the full compiler
David> always invoked as well at run time?

The only mechanism to do that already still pulls in all of the Perl
runtime, including the compiler.  There is no partitioning of Perl, or
subsetting of Perl.

If you're trying to "compile" a Perl program, be sure you refer to the
FAQ.  90% of the people who are trying to "compile" a Perl program
will get no win for the things they think "compiling" will gain them.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095

Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!



Sat, 06 Sep 2003 11:39:53 GMT  
 Is there throw/try/catch semantics in Perl?

David> However, I have picked up the distinct impression that 'eval'
David> invokes the full Perl compiler at run time (am I correct?).

eval STRING does.  eval BLOCK doesn't.

David> This is not-so-subtly different than having the 'eval'
David> statement compiled along with the rest of the code, especially
David> when heavy looping is involved.  Thus the 'eval' semantics are
David> not quite the same as C++'s or Java's (again, especially in
David> light of heavy looping). In the cases that I am currently
David> programming, the 'eval' statements are known at compile time
David> (not compiling statements inside variables/expressions).

However, as has been said elsewhere in this thread, the full compiler
is always in your process anyway, since s///e requires it, as does
"do" and therefore "require" and "use".

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095

Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!



Sat, 06 Sep 2003 11:38:09 GMT  
 Is there throw/try/catch semantics in Perl?

Quote:
>Given your statement that the full Perl Compiler is always invoked for the
>script, I am forced to ask what happens when a script containing eval BLOCK
>is converted into a stand-alone executable (say  a .exe on Windows 2K). Is
>the full compiler always invoked as well at run time?

If you have s///e anywhere, it will invoke the full Perl interpreter.

On Win32, perl.exe + perl56.dll are about 660K.
A perl script compiled to a standalone .exe will be about 650K.

C:\>perldoc -q compile
--
See http://www.inwap.com/ for PDP-10 and "ReBoot" pages.



Sat, 06 Sep 2003 16:42:51 GMT  
 Is there throw/try/catch semantics in Perl?

Quote:

> ...compiled just once (which is what I was hoping for since I will
> ultimately convert the script into a stand-alone executable).

> ...what happens when a script containing eval BLOCK
> is converted into a stand-alone executable?

There is no such thing (yet, anyway).  There exist means to package
up your script with the perl interpreter into one .exe bundle, but
ultimately it's just your script and the perl interpreter.  Even then,
the normal compile-then-interpret cycle occurs, exactly as normally.

Perl is not compiled into native object code, as with other languages
like C.  If you were thinking that, you are mistaken.
Please read perlfaq3:
        How can I compile my Perl program into byte code or C?

--
John Porter

Useless use of time in void context.



Sun, 07 Sep 2003 11:54:20 GMT  
 Is there throw/try/catch semantics in Perl?

Quote:

> Hi

> I am new to Perl but not to C++/Java.

> I am looking for a mechanism similar to throw and try/catch semantics in C++
> and Java.

Yes. There is. Take a look at the Error.pm module and write sth. like

try {
        # ...

Quote:
} catch CORBA::Exception with {

  print "[warn] Unexpected CORBA exception: $_[0]\n";
Quote:
} catch Error::Simple with {

  print "[warn] Unexpected error: $_[0]\n";

Quote:
};

(CORBA::Exception arises from CORBA::ORBit module which uses Error.pm,
Error::Simple should catch usual perl exceptions thrown with 'die')

The method above is not (yet) standard, currently perl programs usually

happy.

<I-am-going-to-be-killed-mode>
In case you just search some nice scripting language and just got to
perl by accident, you could take a look at python - this is nice
scripting language with clean implementation of classes, exceptions,
etc.
</I-am-going-to-be-killed-mode>

--
http://www.mk.w.pl /
 Marcin.Kasperski | Internetowy arkusz pracy (timesheet):            




Mon, 08 Sep 2003 01:25:27 GMT  
 
 [ 18 post ]  Go to page: [1] [2]

 Relevant Pages 

1. SQL problem

2. Help!!

3. Try, Throw, Catch?

4. How to die with a non-string (via try/throw/catch)?

5. Question about the SYSTEM functionIn a Perl program I am trying to do the following:

6. try ... catch

7. try & catch

8. eval, catch/try, and parameters

9. Modules for try/catch?

10. TreeView with Images

11. Perl keeps me from throwing up while programming java

12. qw'...' throws off Perl parser

 

 
Powered by phpBB® Forum Software