$v = shift OR$_[0] on a filehandle, whats teh difereence

From: Eduard W. Lohmann (eduard_at_klomp.org)
Date: 11/24/03

  • Next message: Matt Breedlove: "Importing 80+ hashed and 1 array into several perl scripts"
    Date: Mon, 24 Nov 2003 16:57:32 +0100
    
    

    Hello. I write this little thing to help me log from several instances of
    the same class in apache, mod_perl. But I can't figure one thing out.

    package Logger;
    require Carp;

    # Write the message to the file pointed to by $fh with the time prepended
    # and the place appended
    sub log_ {
      my $fh = shift;
      print( $fh scalar(localtime()), ': ', Carp::shortmess("@_") );
    };

    Now this works fine, but if I replace this with:

    sub log_ {
      print( $_[0] scalar(localtime()), ': ', Carp::shortmess("@_") );
    };

    I get print on unopened filehandle. I can't seem figure out why.

    # Turn array into a nice string, like (1, 2, 3) and log it
    sub log_ARRAY {
      log_($_[0], '('.join(', ', @{$_[1]}).')');
    };

    # Turn hash into a nice string, like { 1 => 'one', 2 => 'two' } and log it
    sub log_HASH {
      log_($_[0],'{ '.join(', ', map("$_ => ".$_[1]{$_},sort keys %{$_[1]})).' }');
    };

    # Treat anything else as a string and log it
    sub AUTOLOAD {
      goto &log_ if $AUTOLOAD =~ /^log_/;
    };

    # Logger::method('command', 'filename')
    # Open 'filename' and create a sub in the callers package named 'command'
    # that logs to it, nicely formatted.
    sub method {
      open(my $fh, ">> $_[1]") or $fh = \*STRERR;
      *{ caller().'::'.$_[0]} = sub {
        &{'log_'.ref($_) }($fh, $_) foreach (@_);
      };
    };

    1;


  • Next message: Matt Breedlove: "Importing 80+ hashed and 1 array into several perl scripts"

    Relevant Pages

    • Re: Change Access Background
      ... Private Sub cmdChangeMDI_Click ... ' Since we are not passing a filename of a Bitmap file ... ' the standard Window File Dialog will popup. ... >> Updated May 03,2003 Now allows you to select a Bitmap Image to place in>> the background of the MDI window. ...
      (microsoft.public.access.devtoolkits)
    • Re: Change Access Background
      ... Private Sub cmdChangeMDI_Click ... ' Since we are not passing a filename of a Bitmap file ... ' the standard Window File Dialog will popup. ... >> Updated May 03,2003 Now allows you to select a Bitmap Image to place in>> the background of the MDI window. ...
      (microsoft.public.access.developers.toolkitode)
    • [Full-Disclosure] TINYURL - A powerful tool just got more powerful
      ... After sending huge urls to friends on one to many occasions (at least they ... sub CURL ... Usage $0: filename ... my $filename = shift; ...
      (Full-Disclosure)
    • RE: Saving a file with date and time as filename
      ... msgbox("Cannot Save file - Exiting Sub") ... cancel the routine. ... filename as the defult path and filename. ... oldStatusBar = Application.DisplayStatusBar ...
      (microsoft.public.excel.programming)
    • Re: Help with find and replace macro
      ... I have inserted the word FILENAME in the main text, header and footer. ... I have cut and pasted the below simplified code for sub Test into the Visual ... I then used the debug menu and placed the cursor at the next MyStoryRange ...
      (microsoft.public.word.vba.general)