Exit subroutine on filehandle error

Exit subroutine on filehandle error

am 26.07.2011 11:32:19 von Nikolaus Brandt

Hi,

I'm currently writing a script which contains a subroutine to write
data to files.
Currently I use
open $fh, '>', "$basedir/$userdir/$outfile" or die "Can't write: $!\n";
which has the disadvantage, that the whole script dies if e.g. the
userdir is not available.

Could you give me an advise how to just exit the subroutine if opening
the filehandle fails, without exiting the whole script?

Thanks in advance.

Nikolaus

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Exit subroutine on filehandle error

am 26.07.2011 12:01:54 von Shlomi Fish

Hi Nikolaus,

On Tue, 26 Jul 2011 11:32:19 +0200
Nikolaus Brandt wrote:

> Hi,
>=20
> I'm currently writing a script which contains a subroutine to write
> data to files.
> Currently I use
> open $fh, '>', "$basedir/$userdir/$outfile" or die "Can't write: $!\n";
> which has the disadvantage, that the whole script dies if e.g. the
> userdir is not available.=20
>=20
> Could you give me an advise how to just exit the subroutine if opening
> the filehandle fails, without exiting the whole script?
>=20

To answer your question, look at the return statement:

http://perldoc.perl.org/functions/return.html

You can do:

if (!open my $fh, '>', $path)
{
return;
}

Another option would be to use eval { ... } and $@ to trap exceptions:

http://perl-begin.org/tutorials/perl-for-newbies/part4/#page --exceptions--D=
IR

Now a few comments on your code:

1. Normally, you should do: "open my $fh" instead of "open $fh" to limit its
scope.

2. You're interpolating several variables into a
path: "$basedir/$userdir/$outfile", so make sure you sanitise them. If I pu=
t in
$outfile e.g: "../../../../etc/passwd", then I'll be able to write
to /etc/passwd. See:

http://webcache.googleusercontent.com/search?q=3Dcache:aEbtJ 4YXhVkJ:shlomif=
-tech.livejournal.com/35301.html%3Fthread%3D29157+code+marku p+injection+pre=
vention&cd=3D1&hl=3Den&ct=3Dclnk&source=3Dwww.google.com

(sorry - livejournal.com is down.)

You may also opt to use what Joel Spolsky describes here:

http://www.joelonsoftware.com/articles/Wrong.html

or perhaps a superior method of making the wrong code behave in an obviously
wrong way (i.e: terminate the program with an error), which will require mo=
re
coding in Perl.

Regards,

Shlomi Fish

--=20
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
My Favourite FOSS - http://www.shlomifish.org/open-source/favourite/

Tcl is Lisp on drugs. Using strings instead of Sâ€=90expressions for cl=
osures is
Evil with one of those gigantic Eâ€=99s you can find at the beginning o=
f chapters.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Exit subroutine on filehandle error

am 26.07.2011 12:39:54 von Miquel Ruiz

El 26/07/2011 12:01, Shlomi Fish escribió:
> Another option would be to use eval { ... } and $@ to trap exceptions:
>
> http://perl-begin.org/tutorials/perl-for-newbies/part4/#page --exception=
s--DIR


Important to remember that "open" won't raise an exception if you are=20
not using the "autodie" pragma, which you can enable with:

use autodie;

(http://perldoc.perl.org/autodie.html)

Best regards,

--=20
Miquel Ruiz

--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Exit subroutine on filehandle error

am 26.07.2011 13:04:00 von jwkrahn

Nikolaus Brandt wrote:
> Hi,

Hello,

> I'm currently writing a script which contains a subroutine to write
> data to files.
> Currently I use
> open $fh, '>', "$basedir/$userdir/$outfile" or die "Can't write: $!\n";
> which has the disadvantage, that the whole script dies if e.g. the
> userdir is not available.
>
> Could you give me an advise how to just exit the subroutine if opening
> the filehandle fails, without exiting the whole script?

Yes, just exit (return) from the subroutine:

open my $fh, '>', "$basedir/$userdir/$outfile" or do {
warn "Can't write: $!\n";
return;
};



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Exit subroutine on filehandle error

am 26.07.2011 17:39:25 von Nikolaus Brandt

Hi,

On Tue, Jul 26, 2011 at 01:01:54PM +0300, Shlomi Fish wrote:
> Hi Nikolaus,
>=20
> On Tue, 26 Jul 2011 11:32:19 +0200
> Nikolaus Brandt wrote:
>=20
> > Hi,
> >=20
> > I'm currently writing a script which contains a subroutine to write
> > data to files.
> > Currently I use
> > open $fh, '>', "$basedir/$userdir/$outfile" or die "Can't write: $!\n=
";
> > which has the disadvantage, that the whole script dies if e.g. the
> > userdir is not available.=20
> >=20
> > Could you give me an advise how to just exit the subroutine if openin=
g
> > the filehandle fails, without exiting the whole script?
> >=20
>=20
> To answer your question, look at the return statement:
>=20
> http://perldoc.perl.org/functions/return.html
>=20
> You can do:
>=20
> if (!open my $fh, '>', $path)
> {
> return;
> }
>=20
> Another option would be to use eval { ... } and $@ to trap exceptions:
>=20
> http://perl-begin.org/tutorials/perl-for-newbies/part4/#page --exception=
s--DIR
>=20
> Now a few comments on your code:
>=20
> 1. Normally, you should do: "open my $fh" instead of "open $fh" to limi=
t its
> scope.
Done.
>=20
> 2. You're interpolating several variables into a
> path: "$basedir/$userdir/$outfile", so make sure you sanitise them. If =
I put in
> $outfile e.g: "../../../../etc/passwd", then I'll be able to write
> to /etc/passwd. See:
I added a check to verify there's no naughty stuff going on.
>=20
> http://webcache.googleusercontent.com/search?q=3Dcache:aEbtJ 4YXhVkJ:shl=
omif-tech.livejournal.com/35301.html%3Fthread%3D29157+code+m arkup+injecti=
on+prevention&cd=3D1&hl=3Den&ct=3Dclnk&source=3Dwww.google.c om
>=20
> (sorry - livejournal.com is down.)
>=20
> You may also opt to use what Joel Spolsky describes here:
>=20
> http://www.joelonsoftware.com/articles/Wrong.html
>=20
> or perhaps a superior method of making the wrong code behave in an obvi=
ously
> wrong way (i.e: terminate the program with an error), which will requir=
e more
> coding in Perl.
>=20
> Regards,
>=20
> Shlomi Fish
>=20
> --=20
> ------------------------------------------------------------ -----
> Shlomi Fish http://www.shlomifish.org/
> My Favourite FOSS - http://www.shlomifish.org/open-source/favourite/
>=20
> Tcl is Lisp on drugs. Using strings instead of Sâ€=90expressions fo=
r closures is
> Evil with one of those gigantic Eâ€=99s you can find at the beginni=
ng of chapters.
>=20
> Please reply to list if it's a mailing list post - http://shlom.in/repl=
y .
>=20

Thank you all for the replies.
I used the above mentioned eval-$@ solution which was absolutely
working fine.

Thanks again!

Nikolaus

--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Exit subroutine on filehandle error

am 26.07.2011 17:58:47 von Rob Dixon

On 26/07/2011 16:39, Nikolaus Brandt wrote:
> On Tue, Jul 26, 2011 at 01:01:54PM +0300, Shlomi Fish wrote:
>>
>> Another option would be to use eval { ... } and $@ to trap exceptions:
>
> Thank you all for the replies.
>
> I used the above mentioned eval-$@ solution which was absolutely working fine.

I think Shlomi may have been over-thorough in his list of options. Most
Perl programmers would shudder at the sight of an eval, and in this case
it is an ugly implementation of the try/catch idiom.

John's

> open my $fh, '>', "$basedir/$userdir/$outfile" or do {
> warn "Can't write: $!\n";
> return;
> };

(With or without the warning) will do all that you want, and will
enamour you to all who read your code.

Rob




--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Exit subroutine on filehandle error

am 27.07.2011 09:51:49 von Shlomi Fish

Hi Rob,

On Tue, 26 Jul 2011 16:58:47 +0100
Rob Dixon wrote:

> On 26/07/2011 16:39, Nikolaus Brandt wrote:
> > On Tue, Jul 26, 2011 at 01:01:54PM +0300, Shlomi Fish wrote:
> >>
> >> Another option would be to use eval { ... } and $@ to trap exceptions:
> >
> > Thank you all for the replies.
> >
> > I used the above mentioned eval-$@ solution which was absolutely worki=
ng
> > fine.
>=20
> I think Shlomi may have been over-thorough in his list of options.=20

I've mentioned it for completeness sake.

> Most
> Perl programmers would shudder at the sight of an eval, and in this case
> it is an ugly implementation of the try/catch idiom.
>=20

Eh, why? Have you made a survey that concluded that? I agree that eval { ..=
.. }
if ($@) in Perl has its limitations but using such abstractions as
http://search.cpan.org/dist/Exception-Class/ , it is good enough. And I thi=
nk in
this case, it is appropriate because errors should result in exceptions.

> John's
>=20
> > open my $fh, '>', "$basedir/$userdir/$outfile" or do {
> > warn "Can't write: $!\n";
> > return;
> > };
>=20
> (With or without the warning) will do all that you want, and will
> enamour you to all who read your code.
>=20

Well, one thing I dislike about it is that it is using "or do {...}" instea=
d of
an "if ( ) { ... }". And I did mention something similar.

Regards,

Shlomi Fish

--=20
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
Funny Anti-Terrorism Story - http://shlom.in/enemy

bzr is slower than Subversion in combination with Sourceforge.
â€=94 Sjors, http://dazjorz.com/

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Exit subroutine on filehandle error

am 27.07.2011 10:24:05 von timothy adigun

--0016e6d7ee9b84d1c104a908c5df
Content-Type: text/plain; charset=ISO-8859-1

Hello Nikolaus Brand,
You can try these:
1. ) Instead of "die" in your code use "warn", then return from the
subroutine,
2.) Intstead of hard coding the path and file in your program i.e
["$basedir/$userdir/$outfile" ], ask the user to input the path and file,
assign the input to a scalar and check if it exists/correct or not.
If the path is correct, then proceed into your subrotine and if not, you
ask the user to check their input and try again! I think that will be a
better way to go!
thanks

On Tue, Jul 26, 2011 at 10:32 AM, Nikolaus Brandt wrote:

> Hi,
>
> I'm currently writing a script which contains a subroutine to write
> data to files.
> Currently I use
> open $fh, '>', "$basedir/$userdir/$outfile" or die "Can't write: $!\n";
> which has the disadvantage, that the whole script dies if e.g. the
> userdir is not available.
>
> Could you give me an advise how to just exit the subroutine if opening
> the filehandle fails, without exiting the whole script?
>
> Thanks in advance.
>
> Nikolaus
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>

--0016e6d7ee9b84d1c104a908c5df--

Re: Exit subroutine on filehandle error

am 27.07.2011 18:30:35 von Rob Dixon

On 27/07/2011 08:51, Shlomi Fish wrote:
> On Tue, 26 Jul 2011 16:58:47 +0100 Rob Dixon wrote:
>> On 26/07/2011 16:39, Nikolaus Brandt wrote:
>>> On Tue, Jul 26, 2011 at 01:01:54PM +0300, Shlomi Fish wrote:
>>>>
>>>> Another option would be to use eval { ... } and $@ to trap exceptions:
>>>
>>> Thank you all for the replies.
>>>
>>> I used the above mentioned eval-$@ solution which was absolutely working
>>> fine.
>>
>> I think Shlomi may have been over-thorough in his list of options.
>
> I've mentioned it for completeness sake.

Yes, that is what I assumed. But some of your options are less
appropriate, and I think it would have helped to say so.

>> Most Perl programmers would shudder at the sight of an eval, and
>> inthis case it is an ugly implementation of the try/catch idiom.
>
> Eh, why? Have you made a survey that concluded that? I agree that eval { ... }
> if ($@) in Perl has its limitations but using such abstractions as
> http://search.cpan.org/dist/Exception-Class/ , it is good enough. And I think in
> this case, it is appropriate because errors should result in exceptions.

That is a silly comment. You make many assertions yourself Shlomi, but I
wonder how many surveys you have conducted to establish them? It is
unnecessary to turn this into a battle and certainly not what I
intended, so please stop this rivalry.

>> John's
>>
>>> open my $fh, '>', "$basedir/$userdir/$outfile" or do {
>>> warn "Can't write: $!\n";
>>> return;
>>> };
>>
>> (With or without the warning) will do all that you want, and will
>> enamour you to all who read your code.
>>
>
> Well, one thing I dislike about it is that it is using "or do {...}" instead of
> an "if ( ) { ... }". And I did mention something similar.

What exactly is wrong with "or do {...}"?

I believe it is the best option simply because is is comparable to the
common "open ... or die $!" idiom. The do is there only so that a
warning can be issued as well as the return, and

open my $fh, '>', "$basedir/$userdir/$outfile" or return undef;

would be fine.

Rob



--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Exit subroutine on filehandle error

am 28.07.2011 00:45:31 von derykus

On Jul 27, 9:30=A0am, rob.di...@gmx.com (Rob Dixon) wrote:
> ...
> > Well, one thing I dislike about it is that it is using "or do {...}" in=
stead of
> > an "if ( ) { ... }". And I did mention something similar.
>
> What exactly is wrong with "or do {...}"?
>
> I believe it is the best option simply because is is comparable to the
> common "open ... or die $!" idiom. The do is there only so that a
> warning can be issued as well as the return, and
>

I like do{...} as well but an even simpler alternative in
this case:

open( ... ) or warn "..." and return;

--
Charles DeRykus


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Exit subroutine on filehandle error

am 28.07.2011 01:20:43 von Kevin Spencer

On Wed, Jul 27, 2011 at 9:30 AM, Rob Dixon wrote:
> What exactly is wrong with "or do {...}"?
>
> I believe it is the best option simply because is is comparable to the
> common "open ... or die $!" idiom. The do is there only so that a
> warning can be issued as well as the return

There's nothing wrong with issuing an "or do {...}" especially if you
want to do multiple things on failure.

open(my $fh, '<', $somefile) or do {
log_the_error("Could not open $somefile - $!");
do_something_else();
return;
};

Kevin.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Exit subroutine on filehandle error

am 29.07.2011 16:05:26 von rvtol+usenet

On 2011-07-28 00:45, C.DeRykus wrote:

> open( ... ) or warn "..." and return;

Here you are assuming that warn always returns true. It actually does,
even if the device that it write to is full, but I don't think that is
documented ...

--
Ruud

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Exit subroutine on filehandle error

am 29.07.2011 16:37:27 von Paul Johnson

On Fri, Jul 29, 2011 at 04:05:26PM +0200, Dr.Ruud wrote:
> On 2011-07-28 00:45, C.DeRykus wrote:
>
> > open( ... ) or warn "..." and return;
>
> Here you are assuming that warn always returns true. It actually
> does, even if the device that it write to is full, but I don't think
> that is documented ...

Heh - I tested that, looked at the docs and then read the source too.
You could still break it, but only with XS I think. And if you're doing
that sort of thing all bets are off anyway.

However, the whole construct is too ugly and confusing to live.

IMHO, of course.

--
Paul Johnson - paul@pjcj.net
http://www.pjcj.net

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/