perl newbie question...................

O

:-o

Whats wrong with code excerpt below?

I put file test operators in an array list (arg) and 2 filenames in a 2nd array
list (fname).

I do a test

if ( ("$arg $filename") ne 0)
{
print "yes\n"
}
else
{
print "yes\n"
}




----------------------------------------------
#!/bin/perl -w
@arg=('-e', '-d', '-r', '-w', '-x', '-S');
@is=("exists? ", "directory? ", "readable? ", "writeable? ", "executable? ",
"socket? ");
@fname=($0, "perl.exe");
foreach $filename(@fname)
{
$ndx=0;
foreach $argument(@arg)
{
print "is $filename a ", $is[$ndx++];
if ( ("$argument $filename" ) ne 0)
{
print "yes\n";
}
else
{
print "no\n";
}
}
}
 
J

Joe Smith

:-o said:
Whats wrong with code excerpt below?

if ( ("$arg $filename") ne 0) {}

That's not the syntax for calling a function.
You've created a string. You could use eval() on the
string, but it would be better to use a proper code-ref.
#!/bin/perl -w
@arg=('-e', '-d', '-r', '-w', '-x', '-S');
@is=("exists? ", "directory? ", "readable? ", "writeable? ", "executable? ",
"socket? ");
@fname=($0, "perl.exe");
foreach $filename(@fname)
{
$ndx=0;
foreach $argument(@arg)
{
print "is $filename a ", $is[$ndx++];
if ( ("$argument $filename" ) ne 0)
{
print "yes\n";
}
else
{
print "no\n";
}
}
}

The syntax you need is
$result = $reference_to_function->($arguments);



#!/usr/bin/perl
use strict; use warnings;
my %tests = (
'a directory' => sub { -d shift },
'readable' => sub { -r shift },
'writable' => sub { -w shift },
'executable' => sub { -x shift },
'a socket' => sub { -S shift },
'empty' => sub {!-s shift },
);

foreach my $filename ($^X, $0, '/etc', '/etc/hosts', '/dev/null') {
print "$filename\n";
-e $filename or print " does not exist\n" and next;
foreach my $key (sort keys %tests) {
my $is_not = $tests{$key}->($filename) ? '' : 'not ';
print " $is_not$key;";
}
print "\n";
}

######################################################################

/usr/bin/perl.exe
not a directory; not a socket; not empty; executable; readable; not writable;
test-file.pl
not a directory; not a socket; not empty; executable; readable; writable;
/etc
a directory; not a socket; not empty; executable; readable; not writable;
/etc/hosts
not a directory; not a socket; not empty; not executable; readable; not writable;
/dev/null
not a directory; not a socket; empty; not executable; readable; writable;


-Joe

P.S. Newsgroup comp.lang.perl is defunct. Followup-to has been set to
comp.lang.perl.misc, where you should have posted. Also, meaningless
"Subject:" line has been corrected. -Joe
 
J

Jürgen Exner

:-o said:
Whats wrong with code excerpt below?
@arg=('-e', '-d', '-r', '-w', '-x', '-S');
@fname=($0, "perl.exe");
foreach $filename(@fname)
{ [...]
foreach $argument(@arg)
{ [...]
if ( ("$argument $filename" ) ne 0)

You are concatenating in turn each of the strings from @arg with some other
value, e.g. 'perl.exe' and then doing a numerial comparison.

Because none of the values from @arg starts with a digit and thus none of
the concatenated strings starts with a digit, either, their numerical value
is always zero.
Therefore your condition will always be false.

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,820
Messages
2,569,721
Members
45,509
Latest member
Danish324

Latest Threads

Top