Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] FIle handler fails

Expand Messages
  • Jabir Ahmed
    Thanks Jenda, That was helpful. jab Jenda Krynicky wrote: From: Jabir Ahmed ...
    Message 1 of 7 , Nov 28, 2006
    • 0 Attachment
      Thanks Jenda,

      That was helpful.


      Jenda Krynicky <Jenda@...> wrote: From: Jabir Ahmed <jabirahmed@...>
      > 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: $^E\n";
      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@... === 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: jabirahmed@..., jabirahmed@...
      "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]
    Your message has been successfully submitted and would be delivered to recipients shortly.