473,387 Members | 1,561 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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;
Jul 19 '05 #1
1 2709
"Eduard W. Lohmann" <ed****@klomp.org> wrote in message news:<pa****************************@klomp.org>...
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.
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bill | last post by:
I'm trying to use a hash key as a filehandle like so. #!/usr/local/bin/perl use strict; my %buf = ( 'F00' => 'foo.dat' ); &open_files();
4
by: Stacy Mader | last post by:
Greetings all, I have a VMS binary file with weather data. The record on each line is 1xint(8) 12xint(2) Using the perl unpack function, I can decode the binary file like this: <snip>
388
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's...
11
by: vj | last post by:
Hello group, I am working on a compression tool and saw this puzzling bit shit behaviour on a VC++6.0 compiler. #include <iostream> using namespace std; typedef unsigned char uchar; #define...
7
by: Csaba Gabor | last post by:
I'd like to detect the shift key when a button is "clicked" in Firefox/Mozilla. If the button is clicked with the mouse, no problem. However, if the onclick event is keyboard originated, then my...
12
by: Mick_fae_Glesga | last post by:
OK, the solution to this is probably blindingly obvious to everyone, but... surely it can't be right. I am compiling with borland bcc32 free compiler this piece of code is designed to identify...
3
by: Amy Smith | last post by:
Hello there, I am having a small problem which been challenging me for few days and need help or advice. I am trying to calculate the day-shift for employees based on the time they started and...
1
by: Zairay | last post by:
Hi All, I'm having a problem with the Shell32.dll when I try to open a program from my Access db. When I try to open a program called FalconViewLite from my access database I get an error in...
7
by: freshpetals | last post by:
Hello people..I just wanted to know that can we store number of files in a filehandle ..if yes how could we do that???I did write a program but its just parsing result from the file which I'm giving...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.