Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

avoiding newlines when using system calls.

by Zecho (Hermit)
on Aug 27, 2001 at 12:12 UTC ( #108073=perlquestion: print w/ replies, xml ) Need Help??
Zecho has asked for the wisdom of the Perl Monks concerning the following question:

A lot of what i've been writing in my attempts to learn perl have used
the `date` command. When i try to write it to a file as part
of a longer string it always causes a /n after it so I have been putting
it at the end of the line. Such as
$today = `date +%D`; $reg_data = "$handle|$crpw|$first_name}|$last_name|$email_addy|0|$toda +y"; open trf, ">/tmp/.$handle"; print trf $reg_data; close trf;

This works fine but what if I want to use more than one instance of it and
keep it all on one line?? Secondly will every system call do this?

Comment on avoiding newlines when using system calls.
Download Code
Re: avoiding newlines when using system calls.
by dws (Chancellor) on Aug 27, 2001 at 12:14 UTC
    Try   chomp($today = `date +%D`);
    or better, figure out how to to extract what you need from localtime(), and avoid the overhead of invoking another process.

Re: avoiding newlines when using system calls.
by azatoth (Curate) on Aug 27, 2001 at 12:15 UTC
Re: avoiding newlines when using system calls.
by blakem (Monsignor) on Aug 27, 2001 at 12:17 UTC
    First of all forking date is usually a bad idea... You should look into the Date::Calc or Date::Manip modules. Some people also like the strftime routine in the POSIX module

    That said, if you want to remove the newline from the $today variable, you should use chomp.

    chomp($today = `date +%D`);

    -Blake

Re: avoiding newlines when using system calls.
by Zecho (Hermit) on Aug 27, 2001 at 12:21 UTC
    Thanks very much.. The book Im reading to teach myself doesn't include anything about chomp so I had no idea where to look.
Re: avoiding newlines when using system calls.
by BooK (Curate) on Aug 27, 2001 at 13:42 UTC

    If you want to follow blakem advice and use the POSIX module, here is how to do it. There is no newline here, plus you don't fork an external process just to fetch the date.

    use POSIX qw/ strftime /; # standard module $today = strftime("%D", localtime); # localtime in list context

    You could even do it this other way, but be careful with embedded %-sign in your variables. That might bite you.

    use POSIX qw/ strftime /; open trf, ">/tmp/.$handle"; print trf strftime("$handle|$crpw|$first_name|$last_name|$email_addy|0 +|%D", localtime); close trf;
Re: avoiding newlines when using system calls.
by Beatnik (Parson) on Aug 27, 2001 at 15:02 UTC
    This ofcourse being kinda nitpicking... but may I suggest localtime ? Using shell calls to get the date in a certain format just seems plain kinky to me. Shell calls will not only slow ya down, Taint might freak on em (as it should) and it could break your script if (you don't have access|it's not present on the box).

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://108073]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (10)
As of 2014-09-17 19:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (97 votes), past polls