Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

When I use my $file ..can I still do chomp($file) ? 1

Status
Not open for further replies.

vptl

Technical User
Jan 22, 2004
120
0
0
US
my $file=q(/tmp/fs_list_).$date;
chomp($file);
if ( -f $file) {

trying to understand if I use my <variable> is that the final I canot change it ?
 
Yes, you should be able to "chomp($file);" after declaring $file with "my $file;".

So what seems the be the problem then?

Trojan.
 
Hi Trojan, Thanks for quick response.!
Not sure what is causeing the prob..Ithink chomp is not the prob..which I was thinking abt...but something is not right...
I get

I will create /tmp/fs_list_Fri_Jun__3_12_41_16_EDT_2005
sh: $file: ambiguous redirect
readline() on closed filehandle main::FS_LIST at /tmp/k line 12 (#1)

(W closed) The filehandle you're reading from got itself closed sometime before now.
Check your logic flow.


From my code...

use diagnostics;
my $date=`/bin/date| sed -e s/" "/_/g |sed -e s/:/_/g`;
chomp($date);
$file=q(/tmp/fs_list_).$date;
if ( -f $file) {
system(qq!/bin/rm $file > /dev/null 2>&1!);
} else {
print "I will create $file\n";
system(q!/nas/bin/nas_fs -list | egrep -v 'root|name' | awk '{print $6}'|sed '/^$/d' > $file!);
open (FS_LIST,"$file") ;
while ($fs=<FS_LIST>) {
chomp ($fs);
...
....
 
/bin/date usually returns a value with a newline so you almost always have to chomp it if you don't want that newline.

And you can modify variables after you declare them with "my". In fact, that's the only way you can modify them.
 
I solve the ambigus redirection prob by adding back slash to $file in redirection statment...now next one is the open function fails and I have no clue why ? may be the no. of char limitation ?
================================
readline() on closed filehandle main::FS_LIST at /tmp/k line 12 (#1)

(W closed) The filehandle you're reading from got itself closed sometime before now.
Check your logic flow.
==================================
the code is ..

==================================
open (FS_LIST,"$file") ;
while ($fs=<FS_LIST>) {
chomp ($fs);
==================================
 
test the open function and see what the problem is:

open (FS_LIST,"$file") or die "$!";
 
Results...

Code:
==============================================
use diagnostics;
my $date=`/bin/date| sed -e s/" "/_/g |sed -e s/:/_/g`;
chomp($date);
$file='/tmp/fs_list_'.$date;
if ( -f $file) {
system(qq!/bin/rm $file > /dev/null 2>&1!);
} else {
print "There is no $file exists...safe to create new one..!\n";
system(q!/nas/bin/nas_fs -list | egrep -v 'root|name' | awk '{print $6}'|sed '/^$/d' > $file!);
open(FS_LIST,"$file") or die "$!" ;
close (FS_LIST);
}
===========================================================

Output
==========================================================
There is no /tmp/fs_list_Fri_Jun__3_13_29_40_EDT_2005 exists...safe to create new one..!
sh: $file: ambiguous redirect
Uncaught exception from user code:
No such file or directory at /tmp/k line 11.
===========================================================

ls -l on /tmp/shows no files...related with this prog.

Thanks,.
 
I think the main prob here is interpretation of $file during redirection. I am not sure what will fix this.
I am trying to add/remove quots w/o a clue.
 
Do you actually need this file or just the data stream?

Code:
#!/usr/bin/perl -w
use strict;
use diagnostics;
open STREAM, "/nas/bin/nas_fs -list |" or die "Failed to create nas_fs pipeline";
while(<STREAM>) {
    chomp;
    next unless /(?:root|name)/;
    my @fields = split;
    # Do something with $fields[5] here (should be same as $6 in awk)
    print $fields[5], "\n";
}
close STREAM;

Trojan.
 
I need to genrate a file for the record keeping as to what was my raw material for the test was !
 
ok, what about this?

Code:
#!/usr/bin/perl -w
use strict;
use diagnostics;
open STREAM, "/nas/bin/nas_fs -list |" or die "Failed to create nas_fs pipeline";
my $date = `/bin/date`;
chomp $date;
$date =~ s/[\s:]/_/g;
open OUTFILE, ">/tmp/fs_list_$date" or die "Failed to create log file";
while(<STREAM>) {
    chomp;
    next unless /(?:root|name)/;
    my @fields = split;
    print OUTFILE $fields[5], "\n";  # Record for log file
    # Do something with $fields[5] here (should be same as $6 in awk)
}
close STREAM;

Trojan.


 
still in my out put stream there is a blank line at the end causing prob ...
in other words how do I do this....
next if /(?:root|name|^$)/ ;
 
Works....cool...As alway...thank you SIR!.
 
Just put a separate next statement after the last that says:

Code:
next if /^\s*$/;

Ok? :)

Trojan.

 
My output file is empty....why ?

use diagnostics;
use strict;
my $date=`/bin/date `;
chomp($date);
$date =~ s/[\s:]/_/g;
open STREAM, "/nas/bin/nas_fs -list |" or die "Failed to run nas_fs -list";
open OUTFILE, "> /tmp/fs_list_$date" or die "Failed to create log file";
while (<STREAM>) {
chomp;
next if /(?:root|name|^$)/ ;
my @field = split ;
print "$field[5]\n";
}
close OUTFILE;
close STREAM;
 
Ok now I learned it...I have to do
print OUTFILE "TEXT\N";


Trojan, Thanks a lot for your continued support.
 
No probs.
Also, it's worth noting that doing stuff this way, you only output the records you process (so if the code crashes, you know where it got to) and there's much less shelling out to the system.

All in all, much cleaner and simpler.
I think so anyway. ;)


Trojan.

 
I 100% agree....I am a ksh guy...so it may take a while to clean the slate! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top