Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Append problem!!PLS HELP!

by MelaOS (Beadle)
on Aug 14, 2007 at 02:34 UTC ( [id://632370]=perlquestion: print w/replies, xml ) Need Help??

MelaOS has asked for the wisdom of the Perl Monks concerning the following question:

Hi Fellow monks, I have a weird problem where i've been trying to solve for weeks. I have this function running at the end of a script, and prior to that script moving the /tmp/foo files away and print it out using system lpr. I needed to append this information in.Besides the append, i also have a trigger mail sent out. So my problem is that although i can get the email everytime there's a genuine triggering but the append only works sometime. I'm scratching my head on where or what's wrong with it. Anyone got any good recommendation on where i should look at next? Thanks!!!
sub appendToTsum { my ($trigger) = @_; my $foo = "/tmp/foo"; my $home = "\\\\t3file3\\datools\\tools\\MTP\\"; my $logfile = $home."logappend.txt"; open (LOG, ">>$logfile"); my $time = time(); $time = localtime($time); if(-e $foo){ open (SUM, ">>$foo") or print log "$time: Can't write to $foo" +; print SUM "\n\n*********************************************** +******\n"; print SUM "** $trigger **\n"; print SUM "*************************************************** +**\n"; close (SUM); } close (LOG); }

Replies are listed 'Best First'.
Re: Append problem!!PLS HELP!
by jwkrahn (Abbot) on Aug 14, 2007 at 02:47 UTC
    You are opening the filehandle LOG but you are printing to the filehandle log. Perl is case sensitive.
Re: Append problem!!PLS HELP!
by GrandFather (Saint) on Aug 14, 2007 at 03:08 UTC

    Always use strictures (use strict; use warnings;). The case error pointed out by jwkrahn would have been caught by use warnings;. Consider:

    use strict; use warnings; my $msg = '9:23:17'; open (LOG, ">>", 'delme.txt'); print log "$msg\n"; close LOG;

    Prints:

    Argument "9:23:17\n" isn't numeric in log at noname.pl line 7. 2.19722457733622

    which indicates you were double stung! log is a Perl built in function so definitely not a good name to chose! However, even if you had avoided the built in function:

    use strict; use warnings; my $msg = '9:23:17'; open (LOG, ">>", 'delme.txt'); print Log "$msg\n"; close LOG;

    strictures would still save your bacon:

    Name "main::Log" used only once: possible typo at noname.pl line 7. print() on unopened filehandle Log at noname.pl line 7.

    And of course we also strongly recommend you use the three parameter version of open and check for success.


    DWIM is Perl's answer to Gödel
Re: Append problem!!PLS HELP!
by cdarke (Prior) on Aug 14, 2007 at 04:46 UTC
    Once you correct the file handle case problem you will only get something appended to the log file if you cannot open $foo.

    It is a good idea, when reporting an error from open, to always include $!, which will tell you why you "Can't write to $foo" (actually, you are not trying to write to $foo, you are trying to open the file for append).

    BTW: you can save yourself some keystrokes by using / as the directory separator in your file names, Windows accepts either / or \ (some Windows apps only accept \, but perl is not one of them).

    update: I just realised: you are running on UNIX! Why are you using \ for a directory separator in $home?
Re: Append problem!!PLS HELP!
by jesuashok (Curate) on Aug 14, 2007 at 03:08 UTC
    Hi,

    sub appendToTsum { my ($trigger) = @_; my $foo = "/tmp/foo"; my $home = "\\\\t3file3\\datools\\tools\\MTP\\"; my $logfile = $home."logappend.txt"; open (LOG, ">>$logfile"); my $time = time(); $time = localtime($time); if(-e $foo){ open (SUM, ">>$foo") or print log "$time: Can't write to $foo" +; print SUM "\n\n*********************************************** +******\n"; print SUM "** $trigger **\n"; print SUM "*************************************************** +**\n"; close (SUM); } close (LOG); }
    In the above code even if open ( SUM, ".." ) fails it keeps writting the content into SUM filehandle. Please check that too. That may lead to dodgy.

Re: Append problem!!PLS HELP!
by andreas1234567 (Vicar) on Aug 14, 2007 at 09:57 UTC
    Consider using File::Spec::Functions for operations on file names:
    $ cat 632370.pl use strict; use warnings; use File::Spec::Functions; my $root = File::Spec->rootdir(); my $home = catfile( $root, qw ( t3file3 datools tools MTP ) ); my $logfile = catfile ( $home, q{logappend.txt} ); print $home; print $logfile; __END__
    # Linux $ perl -l 632370.pl /t3file3/datools/tools/MTP /t3file3/datools/tools/MTP/logappend.txt
    # Win32 C:\src\perl\perlmonks\632370>perl -l 632370.pl \t3file3\datools\tools\MTP \t3file3\datools\tools\MTP\logappend.txt
    --
    Andreas
      hi fellow monks, thanks for the prompt reply. Yes actually this is running on an UNIX env. And this script is actually being called on top of another script.
      One funny thing i've noticed is that the script should be running prior to the copy to another directory and also before it's being printed out.
      But what i'm seeing is that the softcopy doesn't have this information appended and the hardcopy does.
      Is the problem really from my script or from the part that runs below it?
      # Moved earlier to accomodate changes into the test summaries #-------------------------------eMarginality Tool extract------------- +------------------------ if ($RUNMODE eq "P" && $PRODUCT =~ /7ALGMV|7KEDMV|7ALGRV|7KEDRV|7RAYT +V/ ) { if(-e '/prod/datools/tools/MTP/emt_extract.pl'){ system "perl /prod/datools/tools/MTP/emt_extract.pl &"; } } unless(-e "$HOME/sbl/orig_sum_dir/$PRODUCT"){ system "mkdir $HOME/sbl/orig_sum_dir/$PRODUCT"; system "chmod 777 $HOME/sbl/orig_sum_dir/$PRODUCT"; } system "cp /tmp/t4sum $HOME/sbl/orig_sum_dir/$PRODUCT/${LOTNO}_${LOC}_ +${TYPE_NUM}${TYPE_LETT}"; unless(-e "$HOME/sbl/printed_sum_dir/$PRODUCT"){ system "mkdir $HOME/sbl/printed_sum_dir/$PRODUCT"; system "chmod 777 $HOME/sbl/printed_sum_dir/$PRODUCT"; } system "cp /tmp/foo $HOME/sbl/printed_sum_dir/$PRODUCT/foo${LOTNO}_${L +OC}_${TYPE_NUM}${TYPE_LETT}"; system "chmod 777 $HOME/sbl/sbl_sum_dir/$PRODUCT/${LOTNO}_${LOC}_$day$ +month${year}_${ww}"; if ($PRODUCT!~/^(8ARD|7KEDR)/) { system "lpr /tmp/foo &"; } else { system "/data/sbl/cchecklot ${LOTNO}_${LOC} $PRODUCT > /tmp/check_ +print"; system "sleep 4"; ## Will check for file exist and PCS triggered before adding the p +cs message if(-e '/tmp/check_print' && $PCS_T == 1 && $SBL_T == 1 && $NUTESTE +D >= 500 && $PCS_FOUND==1 && $TYPE_NUM==1){ &print_pcsmess; } system "lpr /tmp/check_print"; } system "chmod 777 $HOME/sbl/tmp/${LOTNO}_${LOC}_tmp1"; system "chmod 777 /tmp/t4sum"; system "chmod 777 /tmp/foo"; system "chmod 777 /tmp/check_print";

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2024-04-16 07:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found