List Info

Thread: FIle handler fails




FIle handler fails
user name
2006-11-28 09:37:41
eg:
sub writeFile{
    $file=shift;
    $string=shift;
    open (FH,">$file");
    print FH $string;
    close (FH);
}


This function created 0b files but when i replaced the FH
wit NEWFH the same function worked any idea?

Regards

jab


Phone (mobile) : +91-98861 36420
E-Mail: jabirahmedyahoo.com, jabirahmedgmail.com
--
"a single
conversation across the table with a wise man is worth a
month's study of books."
---
 
---------------------------------
Cheap Talk? Check out Yahoo! Messenger's low PC-to-Phone
call rates.

[Non-text portions of this message have been removed]



Unsubscribing info is here: h
ttp://help.yahoo.com/help/us/groups/groups-32.html 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://g
roups.yahoo.com/group/perl-beginner/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http
://groups.yahoo.com/group/perl-beginner/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:perl-beginner-digest@yahoogroups.com 
    mailto:perl-beginner-fullfeatured@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
    perl-beginner-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 
FIle handler fails
user name
2006-11-28 14:44:45
>>>>> "Jabir" == Jabir Ahmed
<jabirahmedyahoo.com> writes:

Jabir> eg:
Jabir> sub writeFile{
Jabir>     $file=shift;
Jabir>     $string=shift;
Jabir>     open (FH,">$file");

Where's your error checking?
At a minimum, always write these as:

        open FH, ">$file" or die "Cannot
create: $!";

until you want to do something other than die.

Jabir>     print FH $string;
Jabir>     close (FH);
Jabir> }

Also, $file and $string should be lexicals (my $file, etc).

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. -
+1 503 777 0095
<merlynstonehenge.com> <URL:http://www.ston
ehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy,
etc. etc.
See PerlTraining.Stonehenge.com for onsite and
open-enrollment Perl training!


Unsubscribing info is here: h
ttp://help.yahoo.com/help/us/groups/groups-32.html 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://g
roups.yahoo.com/group/perl-beginner/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http
://groups.yahoo.com/group/perl-beginner/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:perl-beginner-digest@yahoogroups.com 
    mailto:perl-beginner-fullfeatured@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
    perl-beginner-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 
FIle handler fails
user name
2006-11-28 14:05:46
From: Jabir Ahmed <jabirahmedyahoo.com>
> eg:
> sub writeFile{
>     $file=shift;
>     $string=shift;
>     open (FH,">$file");
>     print FH $string;
>     close (FH);
> }
> 
> 
> This function created 0b files but when i replaced the
FH wit NEWFH
> the same function worked any idea?

Either it was affected by some other FH somewhere else in
your script 
or you had a typo in the name of the filehandle. It's better
to use 
lexicals (assuming reasonably recent perl). And the $file
and $string 
variables should definitely be lexicals as well, you do not
want to 
overwrite a global variable by calling this function, do
you?


sub writeFile{
    my $file=shift;
    my $string=shift;
    open (my $FH,">$file")
      or die "Failed to open/create $file: $^En";
    print $FH $string;
    close ($FH);
}

This way the function is selfcontained and doesn't affect or
depend 
on any outside stuff. It's not able to overwrite any global
variables 
or filehandles.

You should also always test the result of your open()s!

Jenda
===== JendaKrynicky.cz === http://Jenda.Krynicky.cz
=====
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
	-- Terry Pratchett in Sourcery



Unsubscribing info is here: h
ttp://help.yahoo.com/help/us/groups/groups-32.html 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://g
roups.yahoo.com/group/perl-beginner/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http
://groups.yahoo.com/group/perl-beginner/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:perl-beginner-digest@yahoogroups.com 
    mailto:perl-beginner-fullfeatured@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
    perl-beginner-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 
FIle handler fails
user name
2006-11-29 05:46:01
Thanks Jenda,

That was helpful.

jab

Jenda Krynicky <JendaKrynicky.cz> wrote:     
                            From: Jabir Ahmed
<jabirahmedyahoo.com>
 > eg:
 > sub writeFile{
 >     $file=shift;
 >     $string=shift;
 >     open (FH,">$file");
 >     print FH $string;
 >     close (FH);
 > }
 > 
 > 
 > This function created 0b files but when i replaced the
FH wit NEWFH
 > the same function worked any idea?
 
 Either it was affected by some other FH somewhere else in
your script 
 or you had a typo in the name of the filehandle. It's
better to use 
 lexicals (assuming reasonably recent perl). And the $file
and $string 
 variables should definitely be lexicals as well, you do not
want to 
 overwrite a global variable by calling this function, do
you?
 
 sub writeFile{
     my $file=shift;
     my $string=shift;
     open (my $FH,">$file")
       or die "Failed to open/create $file:
$^En";
     print $FH $string;
     close ($FH);
 }
 
 This way the function is selfcontained and doesn't affect
or depend 
 on any outside stuff. It's not able to overwrite any global
variables 
 or filehandles.
 
 You should also always test the result of your open()s!
 
 Jenda
 ===== JendaKrynicky.cz === http://Jenda.Krynicky.cz
=====
 When it comes to wine, women and song, wizards are allowed 
 to get drunk and croon as much as they like.
  -- Terry Pratchett in Sourcery
 
 
     
                       


Phone (mobile) : +91-98861 36420
E-Mail: jabirahmedyahoo.com, jabirahmedgmail.com
--
"a single
conversation across the table with a wise man is worth a
month's study of books."
---
 
---------------------------------
Want to start your own business? Learn how on Yahoo! Small
Business.

[Non-text portions of this message have been removed]



Unsubscribing info is here: h
ttp://help.yahoo.com/help/us/groups/groups-32.html 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://g
roups.yahoo.com/group/perl-beginner/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http
://groups.yahoo.com/group/perl-beginner/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:perl-beginner-digest@yahoogroups.com 
    mailto:perl-beginner-fullfeatured@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
    perl-beginner-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 
[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )