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

Discussion in 'Perl' started by :-o, Oct 6, 2007.

  1. :-o

    :-o Guest

    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";
    }
    }
    }
    :-o, Oct 6, 2007
    #1
    1. Advertising

  2. :-o

    Joe Smith Guest

    Re: Using variable to call a Perl built-in function (file tests)

    :-o wrote:
    > 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
    Joe Smith, Oct 9, 2007
    #2
    1. Advertising

  3. :-o wrote:
    > 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
    Jürgen Exner, Oct 17, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,756
  2. Jerry C.
    Replies:
    8
    Views:
    201
    Uri Guttman
    Nov 23, 2003
  3. PerlFAQ Server

    FAQ 1.4 What are Perl 4, Perl 5, or Perl 6?

    PerlFAQ Server, Jan 23, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    278
    PerlFAQ Server
    Jan 23, 2011
  4. PerlFAQ Server
    Replies:
    0
    Views:
    642
    PerlFAQ Server
    Feb 3, 2011
  5. PerlFAQ Server

    FAQ 1.4 What are Perl 4, Perl 5, or Perl 6?

    PerlFAQ Server, Feb 27, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    271
    PerlFAQ Server
    Feb 27, 2011
Loading...

Share This Page