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

Discussion in 'Perl' started by Eduard W. Lohmann, Nov 24, 2003.

  1. 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;
     
    Eduard W. Lohmann, Nov 24, 2003
    #1
    1. Advertising

  2. Eduard W. Lohmann

    Guest

    "Eduard W. Lohmann" <> wrote in message news:<>...

    > 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.


    perldoc -f print

    This newsgroup does not exist (see FAQ). Please do not start threads here.
     
    , Dec 2, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mit
    Replies:
    0
    Views:
    429
  2. Roberto Gallo

    Shift - byte[] buf shift

    Roberto Gallo, Jan 27, 2004, in forum: Java
    Replies:
    3
    Views:
    2,083
    Thomas Schodt
    Jan 27, 2004
  3. Wenjie
    Replies:
    3
    Views:
    1,044
    Ron Samuel Klatchko
    Jul 11, 2003
  4. Santosh Nayak

    Left Shift / Right Shift Operators

    Santosh Nayak, Nov 30, 2006, in forum: C Programming
    Replies:
    16
    Views:
    1,463
    CBFalconer
    Nov 30, 2006
  5. Sanny
    Replies:
    38
    Views:
    3,451
    Thomas Richter
    Apr 29, 2011
Loading...

Share This Page