LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-30-2013, 04:17 PM   #1
rhugga
LQ Newbie
 
Registered: Jul 2007
Posts: 5

Rep: Reputation: 0
Question Perl Help - Passing args to subs


Ok. I'm pulling my hair out. I know I've written code just like this years ago but my perl has grown rusty. Anyway, simple debug routine that takes 4 arguments - a string and 3 numeric flags.

I call this function and depending upon where I want the output to go I set the 3 flags

debug("WARNING: blah blah", 1, 0, 0);

Here is the function (and yes I define a function prototype for this):
Code:
sub debug($$$$) {

    if ($DEBUG != 1) { return; } # DEBUG is a global var 

    my $_mesg   = shift;
    my $_f1     = shift;    # STDOUT
    my $_f2     = shift;    # STDERR
    my $_f3     = shift;    # LOGFILE

    if ($_f1 == 1) {
        printf STDOUT "%s : %s\n", localtime(), $_mesg;
        return;
    }
    if ($_f2) {
        sleep 1; # STUB
    }
    if ($_f3) {
        sleep 1; # STUB
    }
    return;
}
Here is sample output:
55 : 12
56 : 12
58 : 12

Why is my message displaying as 12

I'm sure this is something stupid, or my lack of sleep but right now this eludes me.

And yes, I know there are hundreds of debug modules I can use but this is concise and simple for what I need this to do. Data:umper and similar modules are overkill for what I want to do here.

Thx

Last edited by rhugga; 01-30-2013 at 04:51 PM.
 
Old 01-30-2013, 05:52 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
Your call to localtime() is providing the first and second arguments to printf(), you're not seeing the message at all as it's too far down the argument list and is dropped. Try creating a string with the time you want first and passing that:

Code:
#!/usr/bin/perl

use strict;
use warnings;

my $DEBUG = 1;
my $today;

sub getDate {
	my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime time;
	$mon += 1;
	$year += 1900;
	$mday = sprintf("%02d", $mday);
	$mon = sprintf("%02d", $mon);
	$today = "$mday/$mon/$year";
}

sub debug {

    if ($DEBUG != 1) { return; } # DEBUG is a global var 

    my $_mesg   = shift;
    my $_f1     = shift;    # STDOUT
    my $_f2     = shift;    # STDERR
    my $_f3     = shift;    # LOGFILE

    if ($_f1 == 1) {
        printf STDOUT "%s : %s\n", $today, $_mesg;
        return;
    }
    if ($_f2) {
        sleep 1; # STUB
    }
    if ($_f3) {
        sleep 1; # STUB
    }
    return;
}

getDate();
debug('WARNING: blah blah', 1, 0, 0);
 
Old 01-31-2013, 07:13 AM   #3
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Instead of crafting another getDate function, you can also use scalar as in:
Code:
printf STDOUT "%s : %s\n", scalar localtime, $_mesg;
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Passing args from C to NASM WarTurkey Programming 5 01-31-2012 04:19 PM
[SOLVED] Assembly for execve passing args... trist007 Programming 3 08-12-2011 01:05 AM
Passing args in terminal window is OK. but isn't when passing in GNOME launcher why? majrys1962 Debian 0 11-18-2008 06:00 PM
Passing args in an IOCTL neerajb Linux - Software 0 04-20-2006 05:51 AM
perl: passing 2 individual arrays/lists as args johnMG Programming 12 04-18-2005 03:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration