Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Help with stdout.... in perl..

by fdezmichael (Initiate)
on Nov 13, 2009 at 13:35 UTC ( #806944=perlquestion: print w/replies, xml ) Need Help??
fdezmichael has asked for the wisdom of the Perl Monks concerning the following question:

Hi... i have this code....
#!/usr/bin/perl -w use Mail::Internet; require Mail::Address; use Mail::Header; use File::Find; my $dir = "/var/spool/cyrus/mail/domain/r/domain.tld"; find(\&find_emails => $dir); sub find_emails { return unless /\A[0-9]\.\z/; return unless -f $File::Find::name; process_an_email($File::Find::name); return; # print $File::Find::name, "\n"; } $fecha =`date +%F--%k:%M`; sub process_an_email { my ($file) = @_; open(FILE, $file); $mi_obj = new Mail::Internet(\*FILE); close(FILE); $mi_obj->add("Procesado-por-script", "Si"); $mi_obj->add("Fecha-de-Procesamiento", \$fecha); $mi_obj->head->replace("Subject", "Tengo hambre....."); @cuerpo = ("\nEsta es una prueba... reemplazando el cuerpo\n"); $mi_obj->body( \@cuerpo); open(STDOUT, '>', "$file\.new"); $mi_obj->print_header(\*STDOUT); $mi_obj->print_body(\*STDOUT); chown(104, 8, "$file\.new"); }

Everything is working as I need, but now i need to add other header with the date... an that is in the variable $fecha, but every time i execute the script in the header: Fecha-de-Procesamiento: i Got: Fecha-de-Procesamiento: SCALAR(0x88deda8) However if i change: this line: $mi_obj->add("Fecha-de-Procesamiento", \$fecha); By: $mi_obj->add("Fecha-de-Procesamiento", $fecha); when i execute the script i got: Use of uninitialized value $line in substitution (s///) at /usr/share/perl5/Mail/ line 175. Use of uninitialized value $line in substitution (s///) at /usr/share/perl5/Mail/ line 175. Use of uninitialized value $line in substitution (s///) at /usr/share/perl5/Mail/ line 175. Use of uninitialized value $line in substitution (s///) at /usr/share/perl5/Mail/ line 175. So... How can i show the date in the format that i need??? And other question is: Why everything that I print is not going to STDOUT... because i am redirecting to a variable $file?, is there a way to redirect the STDOUT only in the sub process_an_email...? and in the rest keep it working as usual? Thanks... a lot Michael.-

Replies are listed 'Best First'.
Re: Help with stdout.... in perl..
by Unforgiven (Hermit) on Nov 13, 2009 at 14:34 UTC
Re: Help with stdout.... in perl..
by BioLion (Curate) on Nov 13, 2009 at 14:44 UTC

    Firstly, welcome to PerlMonks! And some pointers - please wrap your scripts in < code >< /code > tags and your text in < p >< /p > tags (there are other helpful links and markup tips underneath the preview button when you post anything).

    Firstly part II : use warnings and use strict ( Use strict warnings and diagnostics or die ).

    Secondly, the differences you see here

    However if i change: this line: $mi_obj->add("Fecha-de-Procesamiento", \$fecha); By: $mi_obj->add("Fecha-de-Procesamiento", $fecha);
    is caused by you passing a reference (defined) to a variable (undefined) (first case) and an undefined variable (second case) to your object. See perlref for help on references. It is always a good idea to check for success on system calls ( see Best method to capture return code from system calls? ). But for your purpose it may even be a better idea to use one of the many date / time modules or core functions ( Date::Time, Time::Format, localtime scalar(localtime) ).

    I am not sure I understand your second question, but it might be worthwhile looking at local for how to make any changes local to your sub. Alternately, you can manually undo the redirect at the end of your sub. There are many posts in the monastery on redirecting / manipulating STDOUT / STDIN / STDERR if you have a look around.

    Just a something something...
Re: Help with stdout.... in perl..
by vitoco (Friar) on Nov 13, 2009 at 15:23 UTC

    Hola Michael. ˇBienvenido!

    As it was said, you must move the $fecha assignment just before the find(), but I guess a chomp should be needed:

    my $fecha = `date +%F--%k:%M`; chomp($fecha);

    A sub may appear anywhere in your code, but it will be used when it is called, so your $fecha assignment is logically misplaced. If you wanted to get a new date for each processed file, put the previous lines inside a subroutine.

    Why are you using STDOUT to create new files? Just recycle FILE or use another fd, and close() it before chown().

    Also, use strict and use warnings when writing and testing new code. I've learned too much from them.

Re: Help with stdout.... in perl..
by FalseVinylShrub (Chaplain) on Nov 13, 2009 at 14:46 UTC


    Others have made useful suggestions which I think you should consider.

    Regarding the particular problem you have, I think moving the line that sets $fecha to the output of the date command up above the call to find() should solve it...



Re: Help with stdout.... in perl..
by Anonymous Monk on Nov 13, 2009 at 14:29 UTC


    This doesn't answer your question, but you seem to have formatted your post in one big block of code. You should probably try to put just the blocks of code into <code> tags, and put your regular paragraphs into <p>'s.

    Actually, I do have one comment about your code: you probably should put use strict at the top (and note that use warnings (which I believe has lexical scope) has mostly replaced the -w usage.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://806944]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2017-03-24 02:40 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (295 votes). Check out past polls.