Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

die my own way?

Status
Not open for further replies.

tchatzi

Technical User
Dec 15, 2004
744
GR
when we put in our program something like this:

open FILE, ">$file" or die "Cant open file: $!";

if the program dies , it will print for example something like:

Cant open file: myfile at c:\perlcode\file.pl line 100

but how can i prevent from printing 'at c:\perlcode\file.pl line 100'
and print only my line?

Cant open file: myfile

Thanks for your help.
 
Change:

open FILE, ">$file" or die "Cant open file: $!";


TO:

Code:
open FILE, ">$file" or die "Cant open file: $file";


Michael Libeson
 
It does work, maybe because I use win32-gui?
and the message is not printed in the console window but on a window which it is mayde by it self( i mean i didnt make any window to print a messege, i just wrote this line and the window pops up by itself)
Any other suggestions?

 
Are you running your perl code from the command line or are you executing the perl script from within explorer?

If you are executing the perl script from explorer (NOT IE), is this when a new command window is being displayed?


Michael Libeson
 
The correct (portable) answer is to add a newline charater to the end of the string:

Code:
open FILE, ">$file" or die "Cant open file: $!\n";

Barbie
Leader of Birmingham Perl Mongers
 
nice catch Barbie. I normally don't leave out "\n", just have lots on the mind.

Thank you for catching that.


Michael Libeson
 
What would i do without you guys,
Thanks a lot.(only in perl could be something that simple)
I'm starting to fall in love with this Language.
Now the error window shows only my phrase
without the bla bla from windows.
I appreciate!
:)
 
Just 4 clarification
$! is actually a perl special variable for debugging and it's available on all other OS's AFAIK

in verbose mode it can be referred to as
$OS_ERROR
--Paul

cigless ...
 
The special variable $! is the operating system error string. This usually contains things like 'file does not exist'. The question that was asked was to remove 'at c:\perlcode\file.pl line 100' from the string that die() emits. You do this by adding '\n' to the end of the string. Personally I use:

Code:
open FILE, ">$file" or die "Cant open file [$file]: $!\n";

What is verbose mode? I think you mean to 'use English', which is perhaps one of the worst things you can recommend to a beginner. It means every regex in their script gets evaluated for the pre-match, match and post-match variables, and as a result can add a significant overhead to you script. See Nick Clark's slides from his When perl is not quite fast enough talk, specifically the bit about regexps.

Barbie
Leader of Birmingham Perl Mongers
 
\apologies Barbie

verbose I know what I meant- bad selection of book on my part, and damn my brain for remebering the crap stuff ...

4 clarification
just pointing out to perluserpengo that $! is a special variable, and that's why the error message was the way it was.

But enough of this idle banter, with redundant repetitive reinforcement its safe to say that pengo knows about special perl variables now ;-)

Regards
--Paul

PS Welcome Back again


cigless ...
 
Thanks for the lessons you're giving me.
I appreciate your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top