Sponsored Content
Top Forums Shell Programming and Scripting Perl and Sockets - Error handling Post 302400386 by drl on Wednesday 3rd of March 2010 05:22:45 AM
Old 03-03-2010
Hi.
Code:
               In the second form, the code within the BLOCK is parsed only
               once--at the same time the code surrounding the "eval" itself
               was parsed--and executed within the context of the current Perl
               program.  This form is typically used to trap exceptions more
               efficiently than the first (see below), while also providing
               the benefit of checking the code within BLOCK at compile time

-- excerpt from perldoc -f eval

cheers, drl
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

file handling problem in perl......

Hi, I am opening a file......then i am wrting some data into it......and i am reopening the file again but ......i get a error cannot open file....... $::file= "\adder\testfile.txt" open(TEST1,$::file); some write operation close(TEST1) open(TEST1,$::file) 'I GET A ERROR CAN OPEN... (2 Replies)
Discussion started by: vivekshankar
2 Replies

2. Shell Programming and Scripting

Signal handling in Perl

Guys, I'm doing signal handling in Perl. I'm trying to catch ^C signal inside the script. There two scripts : one shell script and one perl script. The shell script calls the perl script. For e.g. shell script a.sh and perl scipt sig.pl. Shell script a.sh looks something like this :... (6 Replies)
Discussion started by: obelix
6 Replies

3. Programming

XML Handling in Perl

Hi there, I'm newby in perl and XML. I can read and parse Xml with XML-Node upper XML::Parser, but how can I create XML tags and pack my individual data in it then send through socket. PLZ lead me :) Thanks in Advance. (1 Reply)
Discussion started by: Zaxon
1 Replies

4. Shell Programming and Scripting

special characters handling in perl

Hi, Here is my piece of code-- sub per_user_qna_detail { for($index=0;$index<@records;$index++) { if($records =~ m/^(.*)\s*Morocco.*Entering\s*Module::authenticate/) { printf "INSIDE per_user_qna_detail on LINE NO $index\n"; $Time_Stamp = $1;... (0 Replies)
Discussion started by: namishtiwari
0 Replies

5. Shell Programming and Scripting

Handling Parameters in Perl

Hi All, I'm pretty new to the forum and also to UNIX. I have a requirement for which I need some help. I have a script (example.script) where I get user inputs using the read command. I would need to pass the read-fetched input to a perl command (explained below) in my script. The part which... (3 Replies)
Discussion started by: bharath.gct
3 Replies

6. Infrastructure Monitoring

Perl Error Handling Problem

I can get this working, but if something is down I get an error and the script does not move on. I can not get the "else" function working. What might I be doing wrong? use SNMP::Simple my %ios = (); $list="list.list"; open(DAT, $list) || die("Can't Open List"); @raw_data=<DAT>;... (4 Replies)
Discussion started by: mrlayance
4 Replies

7. Programming

Perl help for file handling

$# some text $$ some text $@ some text $$. some text Mg1 some text Mg2 some text . . . Mg10 some text The above 10 lines are to be extracted except the lines starting from $#,$$.,... (4 Replies)
Discussion started by: baig.abdul
4 Replies

8. Shell Programming and Scripting

PERL error handling

I have a PERL command line embedded in a UNIX script. The script doesn't handle errors coming out of this command. I'm processing large files and occassionally I run out of disk space and end up with half a file. perl -p -e 's/\n/\r\n/g' < TR_TMP_$4 > $4 How do I handle errors coming out... (1 Reply)
Discussion started by: OTChancy
1 Replies

9. UNIX for Dummies Questions & Answers

Error Handling using ISQL for oracle connection in Perl

Hi Am making connection to oracle using ISQL as shown in the code. This code is just a minor part of a big code. I want to capture the error if the password/login is wrong or if connection is not made. I need to capture the error code also. Also, If such an error occurs, i need to exit out... (4 Replies)
Discussion started by: irudayaraj
4 Replies

10. Shell Programming and Scripting

Perl file handling error

Hi, I am reading and file and writting each word to other file. where I have used array to store the data. I am getting below error as "Use of uninitialized value in concatenation (.) or string at customize_split_raw.pl line 51, <IN_FILE> " Where my line 51 code is 50 foreach... (8 Replies)
Discussion started by: Beginer123
8 Replies
Guard(3pm)						User Contributed Perl Documentation						Guard(3pm)

NAME
Guard - safe cleanup blocks SYNOPSIS
use Guard; # temporarily chdir to "/etc" directory, but make sure # to go back to "/" no matter how myfun exits: sub myfun { scope_guard { chdir "/" }; chdir "/etc"; code_that_might_die_or_does_other_fun_stuff; } # create an object that, when the last reference to it is gone, # invokes the given codeblock: my $guard = guard { print "destroyed! " }; undef $guard; # probably destroyed here DESCRIPTION
This module implements so-called "guards". A guard is something (usually an object) that "guards" a resource, ensuring that it is cleaned up when expected. Specifically, this module supports two different types of guards: guard objects, which execute a given code block when destroyed, and scoped guards, which are tied to the scope exit. FUNCTIONS
This module currently exports the "scope_guard" and "guard" functions by default. scope_guard BLOCK scope_guard ($coderef) Registers a block that is executed when the current scope (block, function, method, eval etc.) is exited. See the EXCEPTIONS section for an explanation of how exceptions (i.e. "die") are handled inside guard blocks. The description below sounds a bit complicated, but that's just because "scope_guard" tries to get even corner cases "right": the goal is to provide you with a rock solid clean up tool. The behaviour is similar to this code fragment: eval ... code following scope_guard ... { local $@; eval BLOCK; eval { $Guard::DIED->() } if $@; } die if $@; Except it is much faster, and the whole thing gets executed even when the BLOCK calls "exit", "goto", "last" or escapes via other means. If multiple BLOCKs are registered to the same scope, they will be executed in reverse order. Other scope-related things such as "local" are managed via the same mechanism, so variables "local"ised after calling "scope_guard" will be restored when the guard runs. Example: temporarily change the timezone for the current process, ensuring it will be reset when the "if" scope is exited: use Guard; use POSIX (); if ($need_to_switch_tz) { # make sure we call tzset after $ENV{TZ} has been restored scope_guard { POSIX::tzset }; # localise after the scope_guard, so it gets undone in time local $ENV{TZ} = "Europe/London"; POSIX::tzset; # do something with the new timezone } my $guard = guard BLOCK my $guard = guard ($coderef) Behaves the same as "scope_guard", except that instead of executing the block on scope exit, it returns an object whose lifetime determines when the BLOCK gets executed: when the last reference to the object gets destroyed, the BLOCK gets executed as with "scope_guard". See the EXCEPTIONS section for an explanation of how exceptions (i.e. "die") are handled inside guard blocks. Example: acquire a Coro::Semaphore for a second by registering a timer. The timer callback references the guard used to unlock it again. (Please ignore the fact that "Coro::Semaphore" has a "guard" method that does this already): use Guard; use Coro::AnyEvent; use Coro::Semaphore; my $sem = new Coro::Semaphore; sub lock_for_a_second { $sem->down; my $guard = guard { $sem->up }; Coro::AnyEvent::sleep 1; # $sem->up gets executed when returning } The advantage of doing this with a guard instead of simply calling "$sem->down" in the callback is that you can opt not to create the timer, or your code can throw an exception before it can create the timer (or the thread gets canceled), or you can create multiple timers or other event watchers and only when the last one gets executed will the lock be unlocked. Using the "guard", you do not have to worry about catching all the places where you have to unlock the semaphore. $guard->cancel Calling this function will "disable" the guard object returned by the "guard" function, i.e. it will free the BLOCK originally passed to "guard "and will arrange for the BLOCK not to be executed. This can be useful when you use "guard" to create a cleanup handler to be called under fatal conditions and later decide it is no longer needed. EXCEPTIONS
Guard blocks should not normally throw exceptions (that is, "die"). After all, they are usually used to clean up after such exceptions. However, if something truly exceptional is happening, a guard block should of course be allowed to die. Also, programming errors are a large source of exceptions, and the programmer certainly wants to know about those. Since in most cases, the block executing when the guard gets executed does not know or does not care about the guard blocks, it makes little sense to let containing code handle the exception. Therefore, whenever a guard block throws an exception, it will be caught by Guard, followed by calling the code reference stored in $Guard::DIED (with $@ set to the actual exception), which is similar to how most event loops handle this case. The default for $Guard::DIED is to call "warn "$@"", i.e. the error is printed as a warning and the program continues. The $@ variable will be restored to its value before the guard call in all cases, so guards will not disturb $@ in any way. The code reference stored in $Guard::DIED should not die (behaviour is not guaranteed, but right now, the exception will simply be ignored). AUTHOR
Marc Lehmann <schmorp@schmorp.de> http://home.schmorp.de/ THANKS
Thanks to Marco Maisenhelder, who reminded me of the $Guard::DIED solution to the problem of exceptions. SEE ALSO
Scope::Guard and Sub::ScopeFinalizer, which actually implement dynamically scoped guards only, not the lexically scoped guards that their documentation promises, and have a lot higher CPU, memory and typing overhead. Hook::Scope, which has apparently never been finished and can corrupt memory when used. Scope::Guard seems to have a big SEE ALSO section for even more modules like it. perl v5.14.2 2011-07-02 Guard(3pm)
All times are GMT -4. The time now is 09:05 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy