Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Write a perl script that takes one argument, the name of a file or directory. The script should call the stat function and print the resulting data along with a one line explanation of each field.

my $args;
$args=@ARGV;

if ($args<1)
{
    die("Expecting a filename");
}

sub showstat
{
      print " $_[0] dev, device number of filesystem\n";
      print " $_[1] ino,      inode number\n";
      print " $_[2] mode,     file mode  (type and permissions)\n";
      print " $_[3] nlink,    number of (hard) links to the file\n";
}

my $filename=$ARGV[0];
if (open (TESTFILE,$filename))
{
    my @filedata= stat(TESTFILE);

    &showstat(@filedata);
    close (TESTFILE);
}
else
{
    print "error on open $filename";
}

My question is if $args=@ARGV, shouldn't $ARGV[0] equal to $args[0] instead of $filename=$ARGV[0]? Why not?

and under where it says @showstat(@filedata), why do you put the ampersand? (i know by definition it is a subroutine), but can I get a clarification what the actual function do in here?

share|improve this question

closed as off topic by Renan, Stéphane Chazelas, Warren Young, Anthon, Ulrich Dangel Jun 6 '13 at 14:09

Questions on Unix & Linux Stack Exchange are expected to relate to Unix or Linux within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Didn't you ask a similar questions a while ago? –  Ulrich Dangel Jun 6 '13 at 14:10
    
@UlrichDangel no, he has asked 3, all closed of course. –  terdon Jun 6 '13 at 14:13
    
@Luc, programming questions are off topic here, try them at stackoverflow.com –  terdon Jun 6 '13 at 14:14

1 Answer 1

Perl is context sensitive. If you assign to a scalar variable

$args = @ARGV

the right hand side value is evaluated in scalar context, i.e. the array returns its size.

Using the ampersand for subroutine calls is a Perl 4 habit. Nowadays, there are only few cases where you really need the ampersand, e.g. in this case, it can be safely deleted.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.