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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

questions about perl [Unix->windows] 2

Status
Not open for further replies.

vietboy505

Technical User
Feb 20, 2006
56
US
Four questions to ask..

#1
Is there a list of UNIX commands that is eqvuivalent to perl? Such as change directory
UNIX: cd
PERL: chdir()
UNIX: mkdir
PERL: mkdir

remove directory
UNIXL rm -r [dir]
PERL: rmdir()

UNIX: pwd
PERL: ? and so on. Much appreciate.. can't seem to find on Google

#2
Is there a way I can fixed that the GUI in Unix will match with Windows side like have have an automatic windows resize to match Windows? The script in Unix works fine, I can see all the GUI, buttons, etc.. but not in Windows, it's out range. Any help?

#3
I have a XY plot draw with some data on. I ran the script in UNIX, it have the data and everything such as x & y axis bar show up. But in Windows, there is no graph grid or data show up? Any one know why?

#4
I have installed ActivePerl on the Windows XP Pro. I tested out the script and it works in UNIX side, but in PC side, it got the error.

Code:
    PHP:
    Tk::Error: Can't set -options to `ARRAY(0x1ce8440)' for Tk::Optionmenu=HASH(
    0x19f22e0): Can't call method "destroy" on an undefined value at script1.pl 
    line 197.
     at C:/Perl/site/lib/Tk/Derived.pm line 294
     Tk callback for event
     Tk callback for .
     Tk callback for .frame
     Tk callback for .frame1
     Tk callback for .frame1.frame
     Tk::Derived::configure at C:/Perl/site/lib/Tk/Derived.pm line 306
     Tk::Widget::new at C:/Perl/site/lib/Tk/Widget.pm line 205
     Tk::Widget::__ANON__ at C:/Perl/site/lib/Tk/Widget.pm line 256
     main::get_input_source at script1.pl line 136
    Can't set -options to `ARRAY(0x1ce8440)' for Tk::Optionmenu=HASH(0x19f22e0):
     Can't call method "destroy" on an undefined value at script1.pl line 197.
     at C:/Perl/site/lib/Tk/Derived.pm line 294

     at C:/Perl/site/lib/Tk/Derived.pm line 306

How can I fixed these errors?
 

1.
unix=pwd
perl=you can use module Cwd;
use Cwd;
$dir = cwd;

2.

2.1. One solution could be ask for the operating system at the beginning of the screen and set the right size values:

#!/usr/bin/perl
# Getting Operating System, Version, date


use warnings;
use strict;

my $date;
my $version;

if ( $^O =~ /Win/ ) {
$version = ( `ver` );
$date = ( `date /t` );
}
else {
$version = ( `uname -r -s` );
$date = ( `date` );
}

print( "The Operating system version is $version");
print( "\n", $date );

2.2 Just get your window width and height and set the right values following these values.

my $width = $window->screenwidth;
my $height = $window->screenheight;


3 and 4:

Post some code......

Cheers

dmazzini
GSM System and Telecomm Consultant

 
what about the UNIX `ls` command eqvuivalent to perl?
 
Depends on what you want to do, really. The glob function, or a combination of opendir, readdir and closedir can be used to get contents of a single directory at a time. If you want to recurse down a directory tree, you'd need a module such as File::Find or File::Find::Rule.
 
Code:
	open (file, "ls -1rt $ARGV[0]* |") || die "quit"; 	open (file, "ls -1 $ARGV[0]* |") || die "quit\n";

That is what I want to replace the ls unix command to a perl command so it can run on the window environment. I will look into File::Find.
Thanks.
 
ishnid,
I can list all the files:

@list = glob("*");
@list = glob("*.pl");

What about I want it to show in 1 column with rever time?

Code:
open (file, "ls -1rt $ARGV[0]* |") || die "quit";
Thanks for the help.
 
I'm not sure what you mean by "rever time".

There are a number of file test operators that will give you information about files - type "perldoc -f -X" on your command line to see those. Alternatively, the stat() function can provide file information also.
 
`ls -1rt $ARGV[0]* `

this one I want it to list the any filename with $ARGV[0]* in 1 entry per line (1), reverse the order (r), sort by the latest of time (t).

I think glob() can't do that.

It can only sort by file extension.
 
This'll do that using a Schwartzian Transform:
Code:
my @sorted_files = map $_->[0],
                   sort { $b->[ 1 ] <=> $a->[ 1 ] }
                   map [ $_, -M ],
                   glob "*.$ARGV[0]";

print $_,"\n" for @sorted_files;
 
Code:
    if ($opt) {
	print("ENTER HERE?");
	#open (list, "ls -1rt $ARGV[0]* |") || die "COULD NOT OPEN SPECIFIED FILE.\n";
    }
    else {
    	#open (list, "ls -1 $ARGV[0]* |") || die "COULD NOT OPEN SPECIFIED FILE.\n";
	
	my @sorted_files = map $_->[0],				# extract original name
		                   sort { 
		                   $b->[2] <=> $a->[2]		# then numerically descending by modtime age (oldest first)
		                   or  $b->[ 1 ] <=> $a->[ 1 ] 	# sort first numerically by size (smallest first)
		                   }	
		                   map [ $_, -M ],		# compute tuples of name, modtime		
		                   glob "*.$ARGV[0]";		# of all files in the directory
		                   #glob "$ARGV[0]*";
		                   
	while(@sorted_files) {
		open (list,$_) || die "COULD NOT OPEN SPECIFIED FILE. Second\n";
	}
print("SUCCESS!");
    }

This is what I did, and it can't open any file. Didn't give the error either. Print out SUCCESS!SUCCESS! only.

What have went wrong?
 
Firstly, you should use "for" rather than "while" to iterate through the @sorted_files array near the end of your code.

The line you've commented out reflects what you were looking for in your previous posts better. The one I added in was due to me misreading what you were looking for.

In the "sort" block, $a and $b are references to arrays that are created in the "map" statement on the line below. The first element in that array is the filename and the second is the modification time. The size isn't contained in it, so as it stands you can't sort by that. Try this:
Code:
  my @sorted_files = map $_->[0],                # extract original name
                           sort {
                           $a->[2] <=> $b->[2]        # then numerically descending by modtime age (oldest first)
                           or  $b->[ 1 ] <=> $a->[ 1 ]     # sort first numerically by size (smallest first)
                           }    
                           map [ $_, -s, -M ],        # compute tuples of name, modtime        
                           glob "$ARGV[0]*";
When you run that, does the @sorted_files array contain the files you're expecting it to? If so, then we can worry about opening those files later.
 
Yeargh. I've reversed the sort in that post. Should be:
Code:
                           sort {
                           $b->[2] <=> $a->[2]        # then numerically descending by modtime age (oldest first)
                           or  $a->[ 1 ] <=> $b->[ 1 ]     # sort first numerically by size (smallest first)
                           }
 
Code:
#!/usr/local/bin/perl

#open (file, "ls -1rt *.pl |") || die "quit hmm\n";
#open (file, "ls -1 *.php |") || die "quit\n";
#@list = glob("*");
#print("Total files: " .@list . "\n"); #print total of files

my @sorted_files = map $_->[0],   # extract original name
  sort {
   $b->[2] <=> $a->[2] # then numerically descending by
 modtime age (oldest first)
   or  $a->[ 1 ] <=> $b->[ 1 ]    # sort first numerically by size (smallest first)
   }
 map [ $_, -s, -M ],       # compute tuples of name, modtime
 #glob "$ARGV[0]*";
 glob "*";

$totalFiles=@sorted_files;
print("Total files: " . $totalFiles . "\n");
print("Files: @sorted_files \n");
for($start=0;$start<=$totalFiles;$start++) {
        print("File name in loop: " . $sorted_files[$start] . "\n \n");
        open ($totalFiles[$start],$_) || die "COULD NOT OPEN SPECIFIED FILE. \n";
        print("Time :" . $start . "\n");
        }

print("SUCCESS!");

That is the full code above, and here is the output:

Code:
vietboy505$ perl ls_file.pl
Total files: 3
Files: ls.php cat.c ls_file.pl
File name in loop: ls.php

COULD NOT OPEN SPECIFIED FILE.
vietboy505$
And for inside ls.php is just a line of:
echo "ls.php";

cat.c
print("cat.c \n");

ls_file.pl
is the codes above...

I wonder why it can't open the file, even though it print out the exact filename. Thanks for your help ishnid!
 
Perhaps I should have been clearer. When I proposed using a for() loop instead of while(), I literally meant to replace the word "while" with the word "for"

With the open() function, the first argument is a name to give the filehandle you're opening. The second is the filename. In your code, the file it's trying to open is the counter for your for() loop, rather than the filename. This should do it:

Code:
for(@sorted_files) {
        print("File name in loop: " . $_ . "\n \n");
        open (FILE,$_) || die "COULD NOT OPEN SPECIFIED FILE. \n";
}
 
Code:
vietboy505$ perl ls_file.pl
Total files: 4
Files: cat.c ls.php d.c ls_file.pl
File name in loop: cat.c

File name in loop: ls.php

File name in loop: d.c

File name in loop: ls_file.pl

SUCCESS!vietboy505$

Awesome, thanks, now this work.
 
Somehow I replace glob "*"; to glob "$ARGV[0]*";
It gives a different output:

Code:
vietboy505$ perl ls_file.pl
Total files: 0
Files:
SUCCESS!
Total files: 4
Files: cat.c ls.php d.c ls_file.pl
File name in loop: cat.c

File name in loop: ls.php

File name in loop: d.c

File name in loop: ls_file.pl

SUCCESS!
vietboy505$
It goes through the command, then goes it again with the for the loop.
 
Is it possible to glob every file extension except *.txt?

So I want glob(*); and not glob(*.txt);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top